{"record":{"id":"9dc35377c8cb8b14","repo":"dotnet/wpf","slug":"sr-reachserialization-cannotreleasexmlwriter","errorCode":null,"errorMessage":"SR.ReachSerialization_CannotReleaseXmlWriter","messagePattern":"SR\\.ReachSerialization_CannotReleaseXmlWriter","errorType":"exception","errorClass":"XpsSerializationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NullPackagingPolicy.cs","lineNumber":118,"sourceCode":"            {\n                _currentDocumentSequenceWriterRef--;\n\n                if(_currentDocumentSequenceWriterRef == 0)\n                {\n                    //\n                    // if any of the other low level writer exist, then\n                    // throw an exception or is it better if we just close\n                    // them and consider that if any additional call on them\n                    // would be the one that throws the expcetion\n                    //\n                    //_currentFixedDocumentSequenceWriter.Commit();\n                    Initialize();\n                    InitializeResourceReferences();\n                }\n            }\n            else\n            {\n                throw new XpsSerializationException(SR.ReachSerialization_CannotReleaseXmlWriter);\n            }\n        }\n\n        /// <Summary>\n        ///\n        /// </Summary>\n        public\n        override\n        XmlWriter\n        AcquireXmlWriterForFixedDocument(\n            )\n        {\n            XmlWriter xmlWriter = null;\n\n            if(_currentFixedDocumentWriterRef == 0)\n            {\n                //\n                // We need to create the corresponding part in the Xps package","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NullPackagingPolicy.cs#L100-L136","documentation":"XpsSerializationException thrown by NullPackagingPolicy.ReleaseXmlWriterForFixedDocumentSequence when internal state does not match an active fixed-document-sequence writer. The policy tracks current writer reference counts/IDs; releasing a writer outside a valid begin/release sequence leaves no matching writer to release, indicating a lifecycle invariant violation.","triggerScenarios":"Calling ReleaseXmlWriterForFixedDocumentSequence (or the packaging policy API that drives it) when no FixedDocumentSequence writer was previously acquired/started, or after it was already released.","commonSituations":"Unbalanced acquire/release calls in custom XPS packaging code; exception paths that skip a Release or double-Release; interleaving document/page/sequence releases out of order in custom PackagingPolicy implementations.","solutions":["Ensure every ReleaseXmlWriterForFixedDocumentSequence call is paired with a prior acquire/start of a fixed-document-sequence writer, with no double release.","Verify release ordering: sequence writers are released only after their contained documents/pages are finished.","Wrap the serialization pipeline so exceptions do not cause a release to run without its matching acquire.","Check any custom NullPackagingPolicy/BasePackagingPolicy overrides for unbalanced writer reference bookkeeping (_currentFixedDocumentSequenceWriterRef)."],"exampleFix":"// before\npolicy.ReleaseXmlWriterForFixedDocumentSequence(); // no sequence writer started\n// after\nif (sequenceWriterAcquired)\n{\n    policy.ReleaseXmlWriterForFixedDocumentSequence();\n    sequenceWriterAcquired = false;\n}","handlingStrategy":"try-catch","validationCode":"if (!fixedDocumentSequenceWriterActive)\n    throw new InvalidOperationException(\"Cannot release FixedDocumentSequence writer: none acquired\");","typeGuard":"bool CanReleaseSequenceWriter(NullPackagingPolicy p) => p.CurrentFixedDocumentSequenceWriter != null;","tryCatchPattern":"try { policy.ReleaseXmlWriterForFixedDocumentSequence(); }\ncatch (XpsSerializationException ex) when (ex.Message.Contains(\"CannotReleaseXmlWriter\"))\n{ /* mark lifecycle state invalid and abort/document the run */ }","preventionTips":["Maintain a boolean/counter in your own code mirroring each acquire/release pair.","Release writers in strict nesting order (page, document, sequence).","Ensure exception handlers do not release writers twice.","Prefer high-level XpsDocument APIs over manual packaging-policy calls."],"tags":["wpf","xps","serialization","state-error"],"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"}