Note that there are some explanatory texts on larger screens.

plurals
  1. POAS3 passing a function as a parameter creates memory leaks
    primarykey
    data
    text
    <p>I have a function that takes another function as a parameter. Something like this :</p> <pre><code>public function onHits(target : Shape, callback : Function) : void </code></pre> <p>I use it by passing a member function as a parameter that should be called whenever the passed target hits something. The function is called many times a frame. So it is used by doing :</p> <pre><code>//code... CollisionManager.onHits(myShape, onHitCB); //code... </code></pre> <p>The on hit function :</p> <pre><code>public function onHitCB(hitObject : *) : void { //removed all code to test this problem } </code></pre> <p>When I do this, I have a memory leak. I've isolated the problem to that onHits method and have commented out everything else. onHits is an empty method with no code inside it, onHitCB is also empty. If I comment out the call to onHits, there is no memory leak and if I pass null instead of onHitCB there is no memory leak.</p> <p>So it's clearly when I pass onHitCB as a parameter that's the problem. So I thought it might be because Flash allocates some memory to create the Function pointer and doesn't release it but I call System.gc() every frame in debug mode and the leak is still there. Which would mean that this is either a bug in the SDK or I'm not doing something right.</p> <p>I have found a weird workaround by keeping a variable that points to the function which I assign in the constructor of my object :</p> <pre><code>private var func : Function; public function MyObject() { func = onHitCB; } </code></pre> <p>and this will clear the memory leak <strong>even if I still pass onHitCB as the parameter</strong>. So that would mean that it's not the "getter" function to obtain the onHitCB but something else causing the memory leak?</p> <p>I'm very confused. How can this cause a memory leak :</p> <pre><code>public function MyObject() { } public function update() : void { CollisionManager.onHits(myShape, onHitCB);//empty function } public function onHitCB(hitObject : *) : void { //removed all code to test this problem } </code></pre> <p>but not this? :</p> <pre><code>private var func : Function; public function MyObject() { func = onHitCB; } public function update() : void { CollisionManager.onHits(myShape, onHitCB);//empty function } public function onHitCB(hitObject : *) : void { //removed all code to test this problem } </code></pre> <p>and is there a way to not have to do this workaround?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload