Skip to content

Purge cached text sprites before releasing a font #441

Description

@botoddly

Problem

FontSystem.ReleaseFont closes the native TTF_Font and frees its native data, but cached text sprites keyed by that Font remain in the text sprite cache. The cache key keeps the released Font object reachable.

If a cached entry later becomes unreachable and is removed, requesting the same text and released Font again misses the cache and RenderTextToSurface calls SDL_ttf with the closed native font pointer. This makes the public Font.Dispose path capable of reaching a use-after-release native handle.

Scope

  • Remove every cached text-sprite entry keyed by a Font before closing that font.
  • Invalidate any surviving borrowed texture handles before releasing their backing textures.
  • Ensure subsequent text creation with a released Font fails predictably before entering SDL_ttf.
  • Keep disposal idempotent.

Tests

  • Releasing one font removes only text sprites belonging to that font.
  • Cached sprites belonging to other fonts remain valid.
  • A retained text asset for the released font is invalidated before its backing texture is released.
  • Creating or measuring text with a released font throws ObjectDisposedException without calling SDL_ttf.
  • Releasing the same font more than once is safe.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions