Skip to content

Fix freeTextureSource() leaving stale entries in textureSourceHashmap - #612

Merged
jfboeve merged 2 commits into
rdkcentral:masterfrom
guilhermesimoes:bugfix/memory-leak
Jul 31, 2026
Merged

Fix freeTextureSource() leaving stale entries in textureSourceHashmap#612
jfboeve merged 2 commits into
rdkcentral:masterfrom
guilhermesimoes:bugfix/memory-leak

Conversation

@guilhermesimoes

@guilhermesimoes guilhermesimoes commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

When gc() is called, it calls freeUnusedTextureSources, which at the end calls _cleanupLookupMap, cleaning up all unused texture sources from textureSourceHashmap.

However, when freeTextureSource is called (either when a texture source is replaced or when someone manually calls texture.free()), the stale texture source is kept inside textureSourceHashmap, leading to a memory leak.

This issue was detected because we use a single text texture for subtitles, and keep changing the text of that texture as playback progresses.

Copilot AI review requested due to automatic review settings July 30, 2026 14:45
@github-actions

github-actions Bot commented Jul 30, 2026

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

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.

Pull request overview

This PR fixes a memory leak in the texture source reuse cache by ensuring freeTextureSource() removes freed texture sources from textureSourceHashmap, matching the cleanup behavior already performed during gc().

Changes:

  • Remove stale textureSourceHashmap entries when freeTextureSource() is called.
  • Minor whitespace cleanup in TextureManager.mjs.

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

@guilhermesimoes

guilhermesimoes commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

Comment thread src/tree/TextureManager.mjs Outdated
Copilot AI review requested due to automatic review settings July 31, 2026 10:16

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Suppressed comments (1)

src/tree/TextureManager.mjs:199

  • When freeing a loaded texture source, VRAM usage is incremented in uploadTextureSource() via _updateVramUsage(textureSource, 1), but freeTextureSource() never decrements it. This can leave Stage.usedVram* stats stale (only increasing) after manual frees / source replacement.

Call _updateVramUsage(textureSource, -1) before _nativeFreeTextureSource() (it needs textureSource.nativeTexture to still be present).

            this._nativeFreeTextureSource(textureSource);
        }

        this.textureSourceHashmap.delete(textureSource.lookupId);

Copilot AI review requested due to automatic review settings July 31, 2026 10:19

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Suppressed comments (2)

src/tree/TextureManager.mjs:198

  • freeTextureSource() deletes from textureSourceHashmap purely by lookupId. If the same lookupId has been re-associated to a different TextureSource (e.g., after a cleanup/reload), this will remove the wrong entry. Guard against falsy ids and only delete when the map currently points to the instance being freed.
        this.textureSourceHashmap.delete(textureSource.lookupId);

src/tree/TextureManager.mjs:195

  • freeTextureSource() updates _usedMemory for managed textures but does not update VRAM usage counters, unlike _freeManagedTextureSource(). This will leave Stage.usedVram* overstated when textures are freed manually or on replacement.
            if (managed) {
                this._addMemoryUsage(-textureSource.w * textureSource.h);
                this._uploadedTextureSources.splice(index, 1);
            }
            this._nativeFreeTextureSource(textureSource);

Copilot AI review requested due to automatic review settings July 31, 2026 11: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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@jfboeve
jfboeve merged commit 8fe7723 into rdkcentral:master Jul 31, 2026
4 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 31, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants