Skip to content

fix(particle): null module-scope vars after construction to prevent memory leak#8809

Merged
mvaligursky merged 1 commit into
playcanvas:mainfrom
Skullheadx:main
May 29, 2026
Merged

fix(particle): null module-scope vars after construction to prevent memory leak#8809
mvaligursky merged 1 commit into
playcanvas:mainfrom
Skullheadx:main

Conversation

@Skullheadx
Copy link
Copy Markdown
Contributor

Description

The module-scope variables setPropertyTarget and setPropertyOptions are used as scratch variables in the ParticleEmitter constructor, but it does not set them to null after using them. This causes a large memory leak in SPA apps because the garbage collector does not free the ParticleEmitter (assigned to setPropertyTarget), which has a reference to graphicsDevice, which adds up to 100+ MB per navigation in my testing.

Changes

Null setPropertyOptions and setPropertyTarget immediately after we are finished using them in the constructor.

Testing

I tested this fix by comparing heap snapshots after a SPA page navigation in a production app, and the change fixed the memory leak. Before the change, the previous page's ParticleEmitter was retained via setPropertyTarget and the SourceTextModule.

Related to #7674

Checklist

  • I have read the contributing guidelines
  • My code follows the project's coding standards
  • This PR focuses on a single change

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Nulls the module-scope scratch references setPropertyTarget and setPropertyOptions after the ParticleEmitter constructor finishes using them, so the most recently constructed emitter (and its graphicsDevice reference chain) isn't retained by the module forever. This addresses a SPA navigation memory leak related to #7674.

Changes:

  • Reset setPropertyTarget and setPropertyOptions to null after the final setProperty call in the constructor.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@mvaligursky mvaligursky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic, thanks for the fix!

@mvaligursky mvaligursky merged commit efec511 into playcanvas:main May 29, 2026
17 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants