# Simple (I think) question about declares

**URL:** <https://ifnotnil.com/t/simple-i-think-question-about-declares/758>\
**Category:** Other\
**Created:** [20 July 2020 08:39 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758 "2020-07-20T08:39:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Garry](https://ifnotnil.com/user_avatar/ifnotnil.com/garry/32/2_2.png) [@Garry](https://ifnotnil.com/u/Garry)\
**Post date:** [20 July 2020 08:39 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758/1 "2020-07-20T08:39:40Z")

</div>

Lot’s of people shy aware from declares because there aren’t a lot of good examples or tutorials on how they work. People just tend to cut and paste examples and hope for the best.

I’m trying to wrap my head around them more clearly (I might even get round to writing a 101 if I succeed) but I have a few simple (macOS) declare-related questions:

#### Getting an instance

Am I right in thinking that this code will get a pointer to a new instance of a class with the specified name. For example, passing “NSColor” as the string will give me back a pointer to a new instantiated NSColor, right?

```auto
Declare Function NSClassFromString Lib "AppKit" (className As CFStringRef) As Ptr
Var newInstance = NSClassFromString("NSColor")

```

#### Properties

The Xojo docs give examples of calling functions and subroutines using declares but not specifically accessing the property of an external class. Am I right in thinking that the general approach is to get an instance of a class (for example, using the `NSClassFromString()` function above and then pass that pointer to another declare whose selector is the name of the property to access?

For example, I currently use this declare in my code (so I know it works). Behind the scenes, is it essentially saying “return to me the `NSColorInstance.redComponent` value”:

```auto
Declare Function NSClassFromString Lib "AppKit" (className As CFStringRef) As Ptr
Var newInstance = NSClassFromString("NSColor")

// Get a property value from newInstance
Declare Function getRedComponent Lib "AppKit" selector "redComponent"(instance As Ptr) As CGFloat
Var red As Integer = getRedComponent(newInstance)

```

#### Memory management

Are there any memory management issues I need to be aware of when messing around with declares? I choose Xojo because it abstracts memory management away but I’m concerned about memory leaks with declares. What happens to these pointers to `NSColor` objects when I’m done with them? Are they freed by Xojo in the normal way?

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [20 July 2020 12:21 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758/2 "2020-07-20T12:21:20Z")

</div>

1. Documentation of the functions, enums and properties is found here.  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/

2. Enable crash on NSException, so when you get it wrong, it will tell you where. The init method of the Ohanaware App Kit will enable this option for you. Otherwise there’s some suggestions here for alternatives.  
[Crash an application on NSException - macOS - Xojo Programming Forum](https://forum.xojo.com/58302-crash-an-application-on-nsexception)

3. Learn to read Objective-C or Swift, so you can take advantage of sample code on the internet, to understand how to use a function.

> [@Garry](#):
>
> `Var newInstance = NSClassFromString("NSColor")`

This gets the Ptr to the NSColor class, it doesn’t create a new instance of the class. You create an instance by calling a factory method on a class (or by allocating memory and using a class constructor). In the header files, class functions (not instance functions) are marked with “+” in front of the name.  
` + (NSColor *)colorWithWhite:(CGFloat)white alpha:(CGFloat)alpha API_AVAILABLE(macos(10.9));`

\*note: not all class functions are factory functions. The header files will tell you which ones are.

> [@Garry](#):
>
> “return to me the `NSColorInstance.redComponent` value”:

Yes. Looking at NSColor.h, it says.  
`@property (readonly) CGFloat redComponent; // Valid only on component based colors whose colorSpace model is NSColorSpaceModelRGB.`

> [@Garry](#):
>
> What happens to these pointers to `NSColor` objects when I’m done with them? Are they freed by Xojo in the normal way?

Xojo make memory management way tooooooo easy! If you continue down this rabbit hole, you’re going to learn about Memory Management!

There’s many rules, but in principle, these are the basics.

1. A factory function, like `+ (NSColor *)colorWithWhite:(CGFloat)white alpha:(CGFloat)alpha` will return an object that is Auto Released. Which means, you can use it in a function and at the end of the run loop, the OS will release it for you.
2. If you want to store that object for use later, you need to “Retain” it and “Release” it when you’re finished.
3. Constructors (which usually start with init) require you to allocate the memory for the ptr before hand. You’re also responsible for releasing the memory on completion.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [20 July 2020 15:23 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758/3 "2020-07-20T15:23:26Z")

</div>

I’d also grab the old versions of MACOSLib from

> **[macoslib/macoslib](https://github.com/macoslib/macoslib)**
>
> MacOSLib - a set of classes to use OS X specific functions in Real Studio / Xojo applications - macoslib/macoslib

and focus on charles code (not a lot of the other cruft that crept in)

It has a very distinct style  
But, correct me if I’m wrong @samRowlands , that he also notes the ownership rules etc that must be followed and does so quite well (personally its a good example of declares done well)

---

<div class="post-metadata">

**Author:** ![Garry](https://ifnotnil.com/user_avatar/ifnotnil.com/garry/32/2_2.png) [@Garry](https://ifnotnil.com/u/Garry)\
**Post date:** [20 July 2020 17:51 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758/4 "2020-07-20T17:51:27Z")

</div>

> [@npalardy](#):
>
> I’d also grab the old versions of MACOSLib

I swear I read somewhere that macOS is 32-bit only? I guess I’m mis-remembering.

---

<div class="post-metadata">

**Author:** ![Garry](https://ifnotnil.com/user_avatar/ifnotnil.com/garry/32/2_2.png) [@Garry](https://ifnotnil.com/u/Garry)\
**Post date:** [20 July 2020 17:52 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758/5 "2020-07-20T17:52:21Z")

</div>

Awesome info @samRowlands - it’s a real help. Thanks.

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [20 July 2020 23:48 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758/6 "2020-07-20T23:48:44Z")

</div>

> [@npalardy](#):
>
> But, correct me if I’m wrong @samRowlands , that he also notes the ownership rules etc that must be followed and does so quite well (personally its a good example of declares done well)

Sorry, don’t 100% understand, are you asking me if the macOSLib explains ownership rules of NSObjects?

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [20 July 2020 23:51 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758/7 "2020-07-20T23:51:23Z")

</div>

Read the headers while you can, swift doesn’t use 'em so eventually they’ll go away and we’ll be left with Apple’s web documentation, which often results in “no overview available”. The headers also list enums in order, which is vital for using them with Xojo.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [21 July 2020 01:56 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758/8 "2020-07-21T01:56:16Z")

</div>

> [@samRowlands](#):
>
> Sorry, don’t 100% understand, are you asking me if the macOSLib explains ownership rules of NSObjects?

more or less yes  
Charles macOSLib does implement the ownership and I think he even has notes about it in the code

---

<div class="post-metadata">

**Author:** ![JeanYves](https://ifnotnil.com/user_avatar/ifnotnil.com/jeanyves/32/4870_2.png) [@JeanYves](https://ifnotnil.com/u/JeanYves)\
**Post date:** [21 July 2020 17:40 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758/9 "2020-07-21T17:40:31Z")

</div>

there are some 64 bits variants out there

> **[vidalvanbergen/macoslib](https://github.com/vidalvanbergen/macoslib)**
>
> MacOSLib - A large collection of classes to use OS X specific functions in Xojo applications - vidalvanbergen/macoslib

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [21 July 2020 20:30 UTC](https://ifnotnil.com/t/simple-i-think-question-about-declares/758/10 "2020-07-21T20:30:35Z")

</div>

yeah I just wish so many hadnt forked and started their own repos and had pushed their stuff back into the main one
