Search code examples
memory-leaksgarbage-collectiondisposehaxeflambe

How to properly dispose objects in Flambe?


In this Flambe guide it says:

"This dispose function removes this component from its owning entity. You should override the dispose-function in order to dispose objects yourself, to prevent memory leaks."

I have 3 questions:

  1. How should I override the dispose function?

  2. How to properly use the dispose function?

  3. Is there a way to check for memory leaks in Flambe?


Solution

  • 1 If you're using Component

     override public function dispose() {
       myReferences = null;
       myDisposable.dispose();
    
       super.dispose();
     }
    

    If you are not using a Component: You can implement Disposable and dispose when needed in another dispose function.

    2 You need to clear references to objects, that means set it to null. You need to close signal connections that are created in that context. You need to dispose the Disposables.

    3 If you use the JavaScript (html) target, you can use the chrome debug inspector / devtools. You can collect memory profiles, observe the cpu usage etc. Really useful! https://developer.chrome.com/devtools/docs/profiles