dotnet/wpf · error · ObjectDisposedException

SR.StorageBasedPackagePropertiesDiposed

Error message

SR.StorageBasedPackagePropertiesDiposed

What it means

CheckDisposed throws ObjectDisposedException when any OLE property read/write is attempted on a StorageBasedPackageProperties that has already been disposed (its package or underlying storage was closed). The library tracks disposal and refuses further property access.

Solutions

  1. Read or write all PackageProperties values before disposing/closing the Package; copy needed values into your own objects.
  2. Keep the Package open (dispose it only when properties are no longer needed).
  3. Check ObjectDisposedException in catch blocks when accessing properties on possibly-closed packages and treat the value as unavailable.
  4. Restructure so PackageProperties access happens inside the same using scope as the Package.

Example fix

// before: properties read after disposal
using (var pkg = Package.Open(path))
{
    props = pkg.PackageProperties;
}
Console.WriteLine(props.Title); // ObjectDisposedException
// after
using (var pkg = Package.Open(path))
{
    var title = pkg.PackageProperties.Title;
}
Console.WriteLine(title);
Defensive patterns

Strategy: try-catch

Validate before calling

bool CanAccess(Package pkg) => pkg != null && !disposedFlags.Contains(pkg); // track your own disposal state
var title = IsPackageOpen(pkg) ? pkg.PackageProperties.Title : null;

Try / catch

try { title = pkg.PackageProperties.Title; }
catch (ObjectDisposedException) { title = null; /* package already closed */ }

Prevention

When it happens

Trigger: Accessing package.PackageProperties properties (Title, Creator, etc.) after the containing Package was closed/disposed, or after the StorageBasedPackageProperties itself was disposed; holding the PackageProperties object past the package lifetime.

Common situations: Disposing the Package in a using block and then touching cached PackageProperties afterwards; closing the package on one thread/UI event while another code path still reads properties; long-lived caches of PackageProperties objects.

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/16237e6fb24141c9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/StorageBasedPackageProperties.cs:862

                            SR.Format(SR.UnknownDocumentProperty, fmtid.ToString(), propId),
                            nameof(propId)
                            );
                }
            }
            else
            {
                throw new ArgumentException(
                    SR.Format(SR.UnknownDocumentProperty, fmtid.ToString(), propId),
                    nameof(fmtid)
                    );
            }
        }

        private void
        CheckDisposed()
        {
            if (_disposed)
                throw new ObjectDisposedException(null, SR.StorageBasedPackagePropertiesDiposed);
        }

        #endregion Private Methods

        //------------------------------------------------------
        //
        //  Private Fields
        //
        //------------------------------------------------------

        #region Private Fields

        private bool _disposed;

        private int _grfMode;  // Mode in which the compound file was opened.

        //
        // Interface to the OLE property sets in the compound file representing

View on GitHub (pinned to 81131a70a4)