Kill a class instance (memory leak fix)

I have a class with a shared property array that holds instances whenever a class is added to a window.

What I did is adding a constructor, that adds the instance to the array.
I added a Destructor that removes the instance.

The problem is, that the Destructor never gets called, in this manner.
So, the stacks keeps building up…

In the class I send notification to all the instances in the array.
I do this with some introspection…invoke tricks. I just call a method in the instance that raises the notification event.

But when the window, where the instance lives, is closed… it will not be able to finish the RaiseEvent. A NillErrorExeption is raised. I checked, and indeed, there were no controls in the window that could receive the notification. In my test I added a simple “Property %PropName% Changed” to a textField.

Did I create a memory leak?

To clarify, the class I made is a new approach for configuration settings.

All the settings are managed by shared properties.
But when the class is dragged on a window or container, it can receive notifications, with events I defined in the class.
Whenever a settings is changed, it would send a notification to all the instances.

But in my test, I added this to multiple windows. And whenever I closed a window, I noticed that the Destructor wasn’t called.

Look into WeakRef. You can store the instances that way and it won’t prevent them from being destroyed in the normal fashion.

2 Likes

Thanks!
I did look in into WeakRef. But I needed to cheak the WeakRef.Value for nil. That told me that the reference was out of scope.

That’s right. The WeakRef itself is persistent even if the object to which it refers has been destroyed.

1 Like