Skip to content

fix(web): keep a cached canvas image's inline data alive - #751

Merged
houko merged 1 commit into
nsgo-dev:mainfrom
houko:fix/web-canvas-image-key
Nov 19, 2026
Merged

houko merged 1 commit into
nsgo-dev:mainfrom
houko:fix/web-canvas-image-key

Conversation

@houko

@houko houko commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Part of #738: the web canvas cache. The Apple canvas cache was fixed in #737; the other caches #738 lists are left to it.

ImageSource.Key names inline data by its backing address and length, and the web canvas image cache (canvasImage) did not keep the source. Once an app dropped an inline Data buffer, the garbage collector could hand its address to a new buffer of the same length, and a canvas or shader drawing the new bytes got the old image. Since the web cache never evicts an entry, that wrong image stayed for the life of the surface. The entry now keeps its source, as the Apple canvas cache does since #737 and the Linux and Windows canvas image slots already did, so the address cannot be reused while its key is cached.

The cache already keeps each decoded Image element for the life of the surface, so this also keeps the encoded bytes it was made from; it does not change how many entries are kept.

Targets

  • Shared code (core, base, ui, tsx, the nsgo tool)
  • macOS
  • iOS
  • tvOS
  • watchOS
  • Android
  • Windows
  • Linux
  • Web
  • Mini programs
  • Docs or website only

Screenshots

None. No visual change for an app whose buffers stay alive.

Verification

  • GOOS=js GOARCH=wasm go vet ./internal/platform/web
  • go tool nsgo test -target web ./internal/platform/web (headless Chromium): 19 passed, 1 skipped, including the new TestCanvasImageKeepsItsInlineData, which checks that the cached entry holds the source's bytes and that the same bytes reuse the entry.

Checklist

@github-actions github-actions Bot added size/S bug Something isn't working Web labels Nov 19, 2026
ImageSource.Key names inline data by its address, and the web canvas image cache did not keep the source, so a collected buffer's address could come back in a new buffer of the same length and be handed the old image, which the cache never evicts. The entry now keeps its source, as the Apple canvas cache does since nsgo-dev#737 and the Linux and Windows canvas image slots already did.

Part of nsgo-dev#738
@houko

houko commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Reviewed independently. The web g.textures table (shader.go) is only filled after s.loadImage succeeds, and canvasImages never evicts and now holds src, so a key cannot be reused for the life of the surface on either the canvas or the shader path. The extra memory is the encoded bytes behind each Image the cache already keeps for the surface's life. Per review, the commit now says "Part of #738" (the other caches listed there are still open) and CHANGELOG.md has a line covering this and #737. go tool nsgo test -target web ./internal/platform/web passes in headless Chromium; CI is green on 30c169d.

@houko
houko merged commit 944d473 into nsgo-dev:main Nov 19, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation size/S Web

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant