class lifecycle/scope

If I do the following (pseudocode)

[code]Dim APPLES() as myClass // GLOBAL

Sub DoThis

Dim myApple as new myClass
myApple.Color = red
myApple.Tasty = true
APPLES.append(myApple)

End Sub[/code]

After execution of DoThis and myApple goes out of scope, what happens to the class in the global array that I appended? Does this get set to Nil or does it remain?

It’ll remain because the global modules APPLES property holds a reference to it.

fyi, you can add a destructor to myClass and see when it’s being garbage collected.

Ah yes a reference but at the end of the Sub, myApples goes out of scope so is the reference just pointing at a Nil object? isn’t APPLES(0) just a pointer to myApple?

I only ask because I am doing a similar thing in my code and appending a few hunred apples to the array. When I check back in the array, the properties are blank/0/empty/false for every Apple. myApple is Dimmed on every cycle of the loop, its properties set and it is appended to the array.

myApple is a PTR to the class
you append the “ptr” to your array… therefore you still have an “in-scope” reference by way of “APPLES”

Xojo uses ARC (automatic reference counting), (somebody correct me if I’m getting this wrong). So myApple is Reference #1, you append it to Apples it is now Reference #2 (pointing to the same object). Your sub completes, myApple is out of scope, leaving you with ONE reference in Apples. should that array be redimmed, go out of scope, or have the indexed object removed, then you reference count goes to zero, and the “apple” is gone…

If you have “apples” but no properties are set, I’d say something else is going on… if you lost the reference, you would have a NIL reference, not a pointer to an object with “blank” properties.

[quote=157610:@Dave S]myApple is a PTR to the class
you append the “ptr” to your array… therefore you still have an “in-scope” reference by way of “APPLES”

Xojo uses ARC (automatic reference counting), (somebody correct me if I’m getting this wrong). So myApple is Reference #1, you append it to Apples it is now Reference #2 (pointing to the same object). Your sub completes, myApple is out of scope, leaving you with ONE reference in Apples. should that array be redimmed, go out of scope, or have the indexed object removed, then you reference count goes to zero, and the “apple” is gone…

If you have “apples” but no properties are set, I’d say something else is going on… if you lost the reference, you would have a NIL reference, not a pointer to an object with “blank” properties.[/quote]

Right, thanks Dave, got it… I’ll dig deeper. I’m probably missing something in my loops. Real headache 8 deep for…next loops with select statements at each level and i’m getting a bit lost… Cant see the wood for the trees.

That tells me you’re not appending to the array you think you are. Or possibly something else is nilling the elements.

Also, when you say…

Dim APPLES() as myClass  // GLOBAL

…you mean a module with an APPLES property right? If you’re dimming that array in a global method then that’s something DoThis doesn’t have access to.

There’s many ways to debug this. IF your array should never have nils then maybe add a CheckAPPLES method and call it from all those places you’re curious about, or even from a running Timer.

Sub CheckAPPLES() for i As integer = 0 to APPLES.Ubound if APPLES(i) = nil then break next End Sub

It’s a global, why doesn’t DoThis have access?

I mean the difference between a property on your module (which is accessible) vs the code “Dim APPLES() as myClass” in a method (which is only accessible in the method).

Found and sorted it thanks folks. A silly mistake in assigning values to my class properties.