{"record":{"id":"dd22ad5ce3b9fa1d","repo":"dotnet/wpf","slug":"basestream","errorCode":null,"errorMessage":"BaseStream","messagePattern":"BaseStream","errorType":"exception","errorClass":"ObjectDisposedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs","lineNumber":282,"sourceCode":"                    }\n                    _refCount = null;\n                    _baseStream = null;\n                }\n            }\n\n            base.Dispose(disposing);\n        }\n\n        public Stream BaseStream\n        {\n            get { return _baseStream; }\n        }\n\n        private void CheckDisposed()\n        {\n            if (IsDisposed)\n            {\n                throw new ObjectDisposedException(\"BaseStream\");\n            }\n        }\n\n        private bool Sync()\n        {\n            System.Diagnostics.Debug.Assert(_baseStream != null, \"Stream has already been disposed\");\n\n            if (_position >= 0 && _position < _length)\n            {\n                if (_position + _offset != _baseStream.Position)\n                    _baseStream.Seek(_offset + _position, SeekOrigin.Begin);\n                return true;\n            }\n\n            return false;\n        }\n\n        private class RefCount","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs#L264-L300","documentation":"SharedStream's CheckDisposed guard throws ObjectDisposedException with objectName \"BaseStream\" when any member (CanRead, CanWrite, Flush, ReadByte, Read, Write, Seek) is used after the stream has been disposed. The underlying base stream was disposed and this shared view is no longer usable.","triggerScenarios":"Using the SharedStream after calling Dispose() or after a using block ended — e.g. reading BAML from a stream closed when its owning resource/file context exited, or a deferred/async read running after disposal.","commonSituations":"Async XAML loading outliving the using-scope that opened the resource stream; disposing a package/resource stream while a Baml2006Reader still holds the SharedStream; double-dispose then reuse.","solutions":["Keep the SharedStream (and its base stream) alive for the whole duration of Baml2006Reader usage — dispose the reader and stream together, reader first.","Move the stream creation and reader consumption into the same scope; await all reads before exiting using.","Guard with stream.CanRead or a disposed flag before reuse if the lifetime is uncertain.","Fix ownership: whoever created the base stream should dispose it last."],"exampleFix":"// before\nSharedStream s;\nusing (var fs = File.OpenRead(path))\n{\n    s = new SharedStream(fs, 0, fs.Length);\n} // fs disposed here\ns.ReadByte(); // ObjectDisposedException\n// after\nusing (var fs = File.OpenRead(path))\nusing (var s = new SharedStream(fs, 0, fs.Length))\n{\n    // consume s fully inside this scope\n    var reader = new Baml2006Reader(s);\n    ...\n}","handlingStrategy":"type-guard","validationCode":"// Guard every use after possible disposal\nif (!stream.CanRead) throw new ObjectDisposedException(nameof(stream));","typeGuard":"static bool IsUsable(Stream? s) => s is { CanRead: true };","tryCatchPattern":"try\n{\n    int b = stream.ReadByte();\n}\ncatch (ObjectDisposedException ex) when (ex.ObjectName == \"BaseStream\")\n{\n    // re-open resource and rebuild the SharedStream if retry is safe\n    throw new InvalidOperationException(\"SharedStream used after disposal; keep stream alive for the reader's lifetime.\", ex);\n}","preventionTips":["Scope the using blocks so SharedStream and Baml2006Reader dispose together (reader first)","Never stash a SharedStream in a field beyond its source stream's using scope","Await all async reads before disposing the source","Check CanRead before reuse when ownership is unclear"],"tags":["io","stream","disposed","lifetime"],"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-21T21:30:21.729Z"}