Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"init" callbacks cause memory leak #414

Open
vicksonzero opened this issue Sep 12, 2024 · 2 comments · May be fixed by #416
Open

"init" callbacks cause memory leak #414

vicksonzero opened this issue Sep 12, 2024 · 2 comments · May be fixed by #416
Labels
bug Bug fixes

Comments

@vicksonzero
Copy link

I am making a simple particle system with the mind that garbage collection shouldn't be a big issue, so i didn't bother to use Pool.

 if (ghost.nextParticle < fixedGameTime) {
      /* #IfDev */
      // console.log(`ghostParticles`);
      /* #EndIfDev */
      ghostParticles.addChild(Sprite({
          x: ghost.x,
          y: ghost.y,
          color: '#000000',
          radius: Math.random() * 3 + 1,
          fadeRate: Math.random() * 0.05,

          update() {
              this.y -= 0.5;
              this.opacity -= this.fadeRate;

              if (this.opacity < 0) ghostParticles.removeChild(this);
          },
      }));


      ghost.nextParticle += (Math.random() * 50) + 20;
  }

However, removeChild isn't sufficient to remove references,
setting this._uf to null helps, but there are still other lingering references.

So i opened the profiler:

image

turns out the init callback still remembers the sprite, as written in https://github.com/straker/kontra/blob/main/src/gameObject.js#L270-L272

this on('init', ()=>...) registers an anonymous callback, so i have no ways to off() it either.

To solve the issue, there are 2 folds:

(1) To just address the issue i am having, it would be nice if we have a clearCallbacks('init') to call, or off('init') without a callback can just delete all callbacks associated to this key (wait isn't that going to wipe callbacks if the user calls off('init', callback) with a null-callback? I dunno, clearCallbacks() may increase the package size tho, or does it not?)

(2) Is to just fix the GameObject destroy code, to properly clear the callback, probably adding 1 more named function so that it can call off() properly.


Quote slack message:
image

Oh really? so you are saying it is probably a bug that init did not get cleared?

@vicksonzero
Copy link
Author

Anyway, my work-around is to just use a Pool, just that i need the Pool object to also be a GameObject, for parent position calculations, so i write it myself instead of using the kontra Pool

@vicksonzero
Copy link
Author

vicksonzero commented Sep 12, 2024

Um... what if your event system supports once('init', ()=> ...), so it will auto clear the callbacks once emit('init') fires. that would be nice to deal with anonymous callbacks.

(jquery's naming is one()

@straker straker added the bug Bug fixes label Oct 2, 2024
@straker straker linked a pull request Oct 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug fixes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants