Self Preservation & Class not Nilled

I don’t know if this is a debugger problem or to be expected.
I ran into a problem in the debugger where a class that was supposed to have been Nilled still existed. I have seen similar cases where something wasn’t executed and it usually means I haven’t restarted XOJO in a while.
Is this something I should expect?
If this is true, how do you protect your processes?

There is probably a reference you don’t know about.

Thanks. I’ll check that.

To add to @Christian_Schmitz 's comment, you might have created a circular reference somewhere (classA holds an instance of classB, and classB holds an instance of classA).

Note that AddHandler with AddressOf counts as a reference. When using classes, use WeakAddressOf.

This class (RndInst) is a single instance so I have everywhere

If RndInst <> Nil then

I only did a cursory check on 277 points, and they sll seem good.

Also since I only use anywhere single instances, I dcn’t have AddHandler or AddressOf.

Probably need to reboot.

You might be able to solve this with a Singleton.

On RndInst, make a private shared property called

mSharedInstance as RndInst

Add a public shared method called Create which mimics the Constructor’s parameters.

Function Create(constructor parameters) As RndInst
  If mSharedInstance = Nil Then
    mSharedInstance = new RndInst(constructor parameters)
  End If

  Return mSharedInstance
End Function

Then make the constructor private.

Now, instead of all that checking for nil, just use Create instead of the constructor. At any given time you’ll only have one instance of the class. Just create local property instances wherever you need to use it and don’t keep your own anywhere.

Var localRnd as RndInst = RndInst.Create

If you want to be able to nil it, make a second public shared method called Reset which just sets the shared property to nil.

mSharedInstance = Nil

Which you would call as


The next time create is called, the shared instance gets reinitialized.

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.