Skip to content

SpreadsheetDocument.Dispose() throws System.ObjectDisposedException #1702

Closed
@sszymons

Description

@sszymons

Describe the bug
SpreadsheetDocument.Dispose() throws System.ObjectDisposedException: Cannot access a closed Stream.

Screenshots
image

To Reproduce

         Stream stream = new MemoryStream();
         var spreadsheetDocument = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook);
         spreadsheetDocument.Save();
         
         stream.Dispose();
         spreadsheetDocument.Dispose();

Observed behavior
Exception is thrown.

System.ObjectDisposedException : Cannot access a closed Stream. at System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc) at System.IO.Compression.ZipArchive.WriteFile() at System.IO.Compression.ZipArchive.Dispose(Boolean disposing) at System.IO.Compression.ZipArchive.Dispose() at System.IO.Packaging.ZipPackage.Dispose(Boolean disposing) at System.IO.Packaging.Package.System.IDisposable.Dispose() at System.IO.Packaging.Package.Close() at DocumentFormat.OpenXml.Features.StreamPackageFeature.Dispose(Boolean disposing) at DocumentFormat.OpenXml.Features.StreamPackageFeature.Dispose() at DocumentFormat.OpenXml.Packaging.PackageFeatureCollection.DocumentFormat.OpenXml.Features.IContainerDisposableFeature.Dispose() at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose(Boolean disposing) at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose()

Expected behavior
Should not throw exception, according to:https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1065

A System.IDisposable.Dispose method should not throw an exception. Dispose is often called as part of the cleanup logic in a finally clause. Therefore, explicitly throwing an exception from Dispose forces the user to add exception handling inside the finally clause.

Desktop (please complete the following information):

  • OS: MasOS 13.5
  • Office version: N/A
  • .NET Target: .NET 6
  • DocumentFormat.OpenXml Version: 3.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions