{"record":{"id":"9f9189bc89bee383","repo":"dotnet/wpf","slug":"current-stream-object-was-closed-and-disposed-cannot-access","errorCode":null,"errorMessage":"Current stream object was closed and disposed. Cannot access a closed stream.","messagePattern":"Current stream object was closed and disposed\\. Cannot access a closed stream\\.","errorType":"exception","errorClass":"ObjectDisposedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs","lineNumber":346,"sourceCode":"\n        #endregion Public Methods\n\n\n        //------------------------------------------------------\n        //\n        //  Internal Methods\n        //\n        //------------------------------------------------------\n        #region Internal Methods\n\n        /// <summary>\n        /// Check whether this Stream object is still valid.  If not, thrown an\n        /// ObjectDisposedException.\n        /// </summary>\n        internal void CheckDisposedStatus()\n        {\n            if (StreamDisposed)\n                throw new ObjectDisposedException(null, SR.StreamObjectDisposed);\n        }\n\n        #endregion Internal Methods\n\n\n        //------------------------------------------------------\n        //\n        //  Internal Methods\n        //\n        //------------------------------------------------------\n        #region Internal Properties\n\n        /// <summary>\n        /// Check whether this Stream object is still valid.\n        /// </summary>\n        private bool StreamDisposed\n        {\n            get","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs#L328-L364","documentation":"ByteStream wraps a Stream for WPF packaging and tracks disposal via a StreamDisposed flag. Any member (Length, Position, Seek, Read) calls CheckDisposedStatus() first and throws ObjectDisposedException once the underlying stream has been closed/disposed. It prevents use-after-dispose on package part streams.","triggerScenarios":"Calling Read, Seek, Position, or Length on a ByteStream after its Stream was closed via Stream.Close()/Dispose(), or after the containing package was closed and disposed its part streams.","commonSituations":"Disposing a Package or FileStream in a using block while still reading XPS/package parts from it; closing the stream in a finally block before a delayed read; sharing a stream between code paths where one disposes it early.","solutions":["Keep the source stream (and its Package) open until all reads of the package part are finished; scope using blocks around all consuming code.","Wrap stream access in try-catch for ObjectDisposedException and re-open the stream if it was closed.","Check the stream's CanRead property before accessing it and reopen/re-create the ByteStream when it is false."],"exampleFix":"// before\nusing (var fs = File.OpenRead(path)) {\n    var bs = new ByteStream(fs);\n    QueueReadLater(bs); // fs disposed here -> later access throws\n}\n// after\nvar fs = File.OpenRead(path);\nvar bs = new ByteStream(fs);\ntry {\n    ReadAll(bs); // consume while stream is open\n} finally {\n    fs.Dispose();\n}","handlingStrategy":"try-catch","validationCode":"if (stream == null || !stream.CanRead) { /* re-open the stream before constructing/using ByteStream */ }","typeGuard":"static bool IsUsable(Stream s) => s != null && (s.CanRead || s.CanWrite);","tryCatchPattern":"try { var len = byteStream.Length; } catch (ObjectDisposedException) { /* re-open stream and retry */ }","preventionTips":["Scope using/dispose blocks around ALL code that touches the package part stream","Never store ByteStream/Stream references beyond the lifetime of their owner Package","Check CanRead before delayed or deferred reads"],"tags":["io","stream","object-disposed","wpf-packaging"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}