stride3d/stride · error · ObjectDisposedException

CharacterBitmap

Error message

CharacterBitmap

What it means

CharacterBitmap.Rows returns the glyph bitmap height in pixels. Stride guards all bitmap accessors with a disposed check and throws ObjectDisposedException naming 'CharacterBitmap' so callers detect use-after-dispose instead of reading freed FreeType memory.

Solutions

  1. Re-fetch the CharacterBitmap from the font instead of using the stale disposed instance
  2. Ensure the owning Font/FontData outlives all code that reads glyph metrics
  3. Dispose the font only after all consumers are finished (e.g. on full shutdown)

Example fix

// before
var rows = cachedBitmap.Rows;
// after
var bitmap = font.GetBitmap(character);
if (bitmap == null) return;
var rows = bitmap.Rows;
Defensive patterns

Strategy: try-catch

Validate before calling

if (bitmap == null || bitmapDisposed) return fallback;

Type guard

bool IsUsable(CharacterBitmap b) => b != null && !b.IsDisposed;

Try / catch

try { rows = bitmap.Rows; }
catch (ObjectDisposedException)
{
    bitmap = font.GetBitmap(character);
    rows = bitmap.Rows;
}

Prevention

When it happens

Trigger: Accessing the Rows property after CharacterBitmap.Dispose() (or its owner FontData/Font being disposed) has been called.

Common situations: Caching CharacterBitmap objects across frames while the font is reloaded; disposing a Font at scene unload while a background thread still reads glyph metrics.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/1cabe5aea4a94826. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Graphics/Font/CharacterBitmap.cs:164

        /// Gets a value indicating whether the <see cref="CharacterBitmap"/> has been disposed.
        /// </summary>
        public bool IsDisposed
        {
            get
            {
                return disposed;
            }
        }

        /// <summary>
        /// Gets the number of bitmap rows.
        /// </summary>
        public int Rows
        {
            get
            {
                if (disposed)
                    throw new ObjectDisposedException("CharacterBitmap", "Cannot access a disposed object.");

                return rows;
            }
        }

        /// <summary>
        /// Gets the number of pixels in bitmap row.
        /// </summary>
        public int Width
        {
            get
            {
                if (disposed)
                    throw new ObjectDisposedException("CharacterBitmap", "Cannot access a disposed object.");

                return width;
            }
        }

View on GitHub (pinned to 96fad776d2)