Skip to content

Fix memory leak when element becomes inactive after src swap - #620

Open
guilhermesimoes wants to merge 2 commits into
rdkcentral:masterfrom
guilhermesimoes:bugfix/texture-manager-memory-leak
Open

guilhermesimoes wants to merge 2 commits into
rdkcentral:masterfrom
guilhermesimoes:bugfix/texture-manager-memory-leak

Conversation

@guilhermesimoes

@guilhermesimoes guilhermesimoes commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

When an element swaps srcs, there's a brief moment in time where it has 2 different textures: this.__texture (new) and this.__displayedTexture (old). When the element becomes inactive, the hook _unsetActiveFlag gets called which does this:

if (this.__texture) {
this.__texture.decActiveCount();
}

But it forgets to do the same for the old (but still displayed) texture! Since the old texture's usage count is not properly decremented, the old texture and its source appear to still be in use, and they get stuck forever in memory. Calling stage.gc() still won't release them, because the source's isUsed will always return true and allowCleanup will always return false.

Copilot AI balanced review requested due to automatic review settings October 8, 2026 17:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new ordering can corrupt texture active counts during re-entrant txUnloaded callbacks.

1 open finding
What changed in this PR

Fixes texture-source retention when an element becomes inactive after swapping textures.

Changes:

  • Reorders active-state handling during texture deactivation.
  • Adds regression coverage for garbage-collecting the previous source.
File Description
src/​tree/​Element.mjs Adjusts texture deactivation ordering.
tests/​textures/​test.textures.js Tests cleanup after a texture swap.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/tree/Element.mjs Outdated
Copilot AI balanced review requested due to automatic review settings October 9, 2026 11:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The unresolved synchronous txUnloaded re-entrancy can leave replacement textures counted as active after the element becomes inactive.

1 open finding

🧠 Review effort: Balanced

Comment thread src/tree/Element.mjs
@@ -406,12 +406,13 @@ export default class Element {
if (this.__texture) {
this.__texture.decActiveCount();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@guilhermesimoes the decActiveCount is called here? so im not sure this is actually a fix..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But the test that I added fails without the current change in Element 🤔 but you're right, let me take a look

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, we're dealing with different textures! This leak only occurs when the element is swapping srcs / textures. I've updated the PR which hopefully should make the issue clearer.

@guilhermesimoes
guilhermesimoes force-pushed the bugfix/texture-manager-memory-leak branch from f4fcb62 to 7fd7485 Compare October 9, 2026 13:18
Copilot AI balanced review requested due to automatic review settings October 9, 2026 13:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused fix preserves inactive event behavior and includes suitable regression coverage.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The active-count fix preserves lifecycle ordering and the regression test covers the reported leak.

0 open findings

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 9, 2026 14:39
@guilhermesimoes
guilhermesimoes force-pushed the bugfix/texture-manager-memory-leak branch from 7fd7485 to afc6819 Compare October 9, 2026 14:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The fix preserves existing lifecycle ordering and directly addresses the leaked active count with regression coverage.

0 open findings

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 9, 2026 14:48
@guilhermesimoes
guilhermesimoes force-pushed the bugfix/texture-manager-memory-leak branch from afc6819 to 7fd7485 Compare October 9, 2026 14:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The active-count fix correctly addresses the leak and includes focused regression coverage.

0 open findings

🧠 Review effort: Balanced

This branch has not been deployed

No deployments
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