question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw ObjectDisposedException if one tries to operate with a disposed workbook

See original GitHub issue

Read and complete the full issue template

Do you want to request a feature or report a bug?

  • Bug
  • Feature
  • Question

Did you test against the latest CI build?

  • Yes
  • No

Version of ClosedXML

e.g. 0.95.3

What is the current behavior?

There was a number of bug reports related to unpredictable behavior when the workbook is accessed after it’s been disposed.

https://github.com/ClosedXML/ClosedXML/issues/1243#issuecomment-585938206 https://github.com/ClosedXML/ClosedXML/issues/1306 https://github.com/ClosedXML/ClosedXML/issues/1491

This is, of course, wrong, but it takes long investigations for the users to nail down the root cause, apparently.

What is the expected behavior or new feature?

The suggestion is to throw ObjectDisposedException each time one tries to operate with a disposed workbook.

See this thread https://github.com/ClosedXML/ClosedXML/issues/1306#issuecomment-597616529

It may break someone’s code, of course, but I think immediately fail is better than have unreliable code with a hidden bug.

Reproducibility

        static void Main(string[] args)
        {
            using (var ms = new MemoryStream())
            {
                var wb = CreateWorkbook();
                wb.SaveAs(ms);

                var wb2 = new XLWorkbook(ms);
                Console.WriteLine(wb2.Worksheet("Test").FirstCell().Value); // <- String.Empty
            }

            Console.ReadKey();
        }

        private static XLWorkbook CreateWorkbook()
        {
            using (var wb = new XLWorkbook())
            {
                var ws = wb.AddWorksheet("Test");
                ws.FirstCell().Value = "Good buy cruel world";
                return wb;
            }
        }
  • I attached a sample spreadsheet. (You can drag files on to this issue)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kenleesecommented, Aug 19, 2020

“If something seems too good to be true, it probably is too good to be true.”

0reactions
Pankratycommented, Aug 19, 2020

Wow, it looks great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot access a disposed object. A common cause of this ...
A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the...
Read more >
ObjectDisposedException - C# in a Nutshell [Book]
This exception is thrown when certain operations are performed on an object that has been disposed. For example, trying to read from an...
Read more >
ObjectDisposedException Class (System)
An ObjectDisposedException is thrown when you try to access a member of an object that implements the IDisposable interface or IAsyncDisposable interface, ...
Read more >
The Case against Throwing ArgumentException and the Like
Throw ObjectDisposedException when a call is made on a member of the disposed object, or; Throw ArgumentNullException when the caller has passed ...
Read more >
Async Void Cannot Access a Disposed Object
A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found