dotnet/wpf · error · ObjectDisposedException

SR.StreamObjectDisposed

Error message

SR.StreamObjectDisposed

What it means

DataSpaceManager's stream (Stream-based wrapper over a compound file data space) throws ObjectDisposedException with message SR.StreamObjectDisposed when used after Dispose. Dispose sets _baseStream to null; CheckDisposed runs at the start of SetLength, Seek, Read, Write, and Flush.

Solutions

  1. Check stream.CanRead/CanWrite (false after dispose) or wrap access in try/catch ObjectDisposedException.
  2. Reopen the stream from the Package/compound file instead of reusing the disposed instance.
  3. Fix object lifetime: keep the owning Package open while the stream is in use; don't cache streams across close.

Example fix

// before
var data = stream.Read(buffer, 0, n); // throws if disposed
// after
if (!stream.CanRead) stream = package.OpenPartStream(); // reopen before use
Defensive patterns

Strategy: try-catch

Validate before calling

bool usable = stream != null && stream.CanRead;

Type guard

bool IsAlive(Stream s) => s != null && s.CanRead;

Try / catch

try { stream.Read(buf, 0, n); } catch (ObjectDisposedException) { stream = ReopenStream(); }

Prevention

When it happens

Trigger: Calling Read/Write/Seek/Flush/SetLength on the data-space stream after its Dispose() was called (explicitly or via using-block exit).

Common situations: Holding a reference to a packaging stream after the containing Package/compound file was closed, or a premature using-scope exit followed by cached-stream reuse.

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 dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/35c8d14d9ac2d881. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/DataSpaceManager.cs:373

                    _baseStream?.Close();
                }
            }
            finally
            {
                _baseStream = null;
                base.Dispose(disposing);
            }
        }

        /////////////////////////////
        // Private Methods
        /////////////////////////////

        private void CheckDisposed()
        {
            if (_baseStream == null)
            {
                throw new ObjectDisposedException(null, SR.StreamObjectDisposed);
            }
        }

        private bool _dirty;
        private Stream _baseStream;
    }

    private struct DataSpaceDefinition
    {
        private ArrayList _transformStack;
        private Byte[]    _extraData;

        internal DataSpaceDefinition(ArrayList transformStack, Byte[] extraData)
        {
            _transformStack = transformStack;
            _extraData = extraData;
        }

View on GitHub (pinned to 81131a70a4)