Search code examples
c#amazon-s3objectdisposedexception

What is a disposed object in C#?


I'm getting an ObjectDisposedException using C#:

System.ObjectDisposedException: Cannot access a disposed object.

Before I get to why I'm getting the error, I'd like to know, what is a disposed object, exactly?

Then, after understanding that, why would this ObjectDisposedException occur, in general? I have a program which uploads files to and downloads files from Amazon S3, and it's giving me this error on very sporadic occasions.


Solution

  • A disposed object is an object that implements IDisposable that has had the Dispose method called. This could be called explicitly or after a using statement completes. If it's happening sporadically, it might be a race condition.