Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpreadsheetDocument.Dispose() throws System.ObjectDisposedException #1702

Open
sszymons opened this issue Apr 3, 2024 · 2 comments
Open

Comments

@sszymons
Copy link

sszymons commented Apr 3, 2024

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
@m-gallesio
Copy link

Have you considered moving from manual .Dispose() to using statements?

@sszymons
Copy link
Author

Have you considered moving from manual .Dispose() to using statements?

Of course. The original implementation use using statement. The example is just simplified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants