{"record":{"id":"81b7974d588e57b4","repo":"dotnet/wpf","slug":"fixedpagereader","errorCode":null,"errorMessage":"FixedPageReader","messagePattern":"FixedPageReader","errorType":"exception","errorClass":"ObjectDisposedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedPageReaderWriter.cs","lineNumber":781,"sourceCode":"        /// </param>\n        /// <param name=\"resourceUri\">\n        /// The absolute path to the resouce to be added. If null\n        /// a unique resource path will be generated\n        /// </param>\n        /// <returns>\n        /// Returns an XpsResource instance for the newly created resource.\n        /// </returns>\n        /// <exception cref=\"ObjectDisposedException\">FixedPageReader has already been disposed</exception>\n        public\n        XpsResource\n        AddResource(\n            Type        resourceType,\n            Uri         resourceUri\n            )\n        {\n            if (null == _metroPart)\n            {\n                throw new ObjectDisposedException(\"FixedPageReader\");\n            }\n\n            //\n            // Create the part and writer\n            //\n            PackagePart metroPart = null;\n            if( resourceUri != null )\n            {\n                metroPart = this.CurrentXpsManager.GetPart(resourceUri);\n                if (metroPart == null)\n                {\n                    metroPart = GeneratePartForResourceType(resourceType, resourceUri);\n                }\n            }\n\n            XpsResource xpsResource;\n            if( resourceType == typeof( XpsImage ) )\n            {","sourceCodeStart":763,"sourceCodeEnd":799,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedPageReaderWriter.cs#L763-L799","documentation":"ObjectDisposedException thrown by XpsFixedPageReaderWriter.AddResource when the page's underlying package part (_metroPart) is null, meaning the FixedPage reader/writer has been disposed (or was never backed by a part). You cannot add resources (images, fonts, etc.) to a page after its part has gone away.","triggerScenarios":"Calling AddResource(Type resourceType, Uri resourceUri) after Commit()ing or disposing the page writer, or on a reader obtained from a closed/disposed XpsDocument.","commonSituations":"Retaining IXpsFixedPageReaderWriter references in collections after the document was closed; adding resources in a deferred/background step that runs after page commit; using statements that dispose the XpsDocument before resource attachment finishes.","solutions":["Call AddResource before Commit()/Dispose of the page writer.","Reorder code so resources (fonts, images) are added while the page is open, then commit last.","Keep the XpsDocument open until all page resources are written.","Remove stale page-writer references once the document is closed; reacquire readers via the document if needed.","Wrap in try-catch for ObjectDisposedException to surface lifecycle bugs during batch generation."],"exampleFix":"// before\npage.Commit();\npage.AddResource(typeof(XpsImage), imageUri); // throws\n// after\npage.AddResource(typeof(XpsImage), imageUri);\npage.Commit();","handlingStrategy":"try-catch","validationCode":"// Track lifecycle: only call while page is open\nif (pageCommitted) throw new InvalidOperationException(\"Page already committed; AddResource is invalid\");\n// else safe:\npageWriter.AddResource(typeof(XpsImage), imageUri);","typeGuard":"bool CanMutatePage(IXpsFixedPageReaderWriter page) => page != null && !isCommitted.Contains(page);","tryCatchPattern":"try\n{\n    pageWriter.AddResource(resourceType, resourceUri);\n}\ncatch (ObjectDisposedException ex) when (ex.ObjectName == \"FixedPageReader\")\n{\n    throw new InvalidOperationException(\"Resources must be added before page Commit(). Reorder the pipeline.\", ex);\n}","preventionTips":["Add all resources (images, fonts, etc.) before calling Commit() on the page.","Keep the XpsDocument open until every page resource is written.","Don't cache page-writer references past document close; reacquire via the document instead.","Structure generation as: open page -> add resources -> set ticket -> commit."],"tags":["xps","wpf","object-disposed","lifecycle"],"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"}