Testing if object reference is valid

I have a component that when instantiated in a client (ie on a componentRunning property event script) will create an global instance of a python class defined in one of my script modules (shared.testing). The event script will pass a reference to the component that instantiated the class to the new class instance.

In shared.testing I have:

def MyClass:
	parentComponent = None

classInstance = MyClass()

In the component propertyChange event script I have:

if event.propertyName == 'componentRunning':
	if event.newValue:
		shared.testing.classInstance.parentComponent = event.source

Later in time I want the class instance to set a property value of the parent component.

shared.testing.classInstance.parentComponent.someProperty = someValue

But I first want to check if the reference to the parent component is still valid. Is this possible? If so, how?

I plan to clear the reference on the same property change event script.

if event.propertyName == 'componentRunning':
	if not event.newValue:
		shared.testing.classInstance.parentComponent = None

But I still want to verify the reference. Just in case.

It’s a python thing. Instance properties in a class must be assigned in the class’s constructor with a self prefix. Your parentComponent property as shown is a class property, not an instance property.
Your module would have to be something like this:

def MyClass:
	def __init__(self):
		self.parentComponent = None

classInstance = MyClass()

However, I would suggest you don’t go down this path. Holding a reference to a gui component in a script module will prevent garbage collection of the window after it is closed, might interfere with caching of the window, and will almost certainly lead to subtle bugs.
If you need a reference to an object in one window from an event in another, a combination of getWindow() and getComponentForPath() in the latter is far safer.

1 Like

Yes I would actually define the instance property as you pointed out. I just messed up when I was writing the code for explanation purposes. Good catch.

Under normal circumstances I would clear the reference to the component in the window before the window closes to avoid these potential problems. But you never know if something will interrupt this process for whatever reason I guess.

Fetching the component each time is certainly a more robust way about it. I was just hoping not to have to do the fetch each time.

So is it true that if I have a reference to an object, a component in a window for example, then that object will never be deleted, destroyed or garbage collected (or whatever the term is) until I clear that reference? Because what I was originally worried about was trying to access an object that isn’t even there anymore. Instead I should be worried about the references causing the opposite problem of preventing objects from being garbage collected?

Correct. If you want to hold a reference that allows deletion & garbage collection, look at the weakref python module.

Is there a way to fetch any objects that are referencing a particular object?

No, not that I know of.