The exception that is thrown when an operation is performed on a disposed object.
Questions tagged [objectdisposedexception]
190 questions
58
votes
3 answers
What should be passed as the objectName when throwing an ObjectDisposedException?
When implementing IDisposable, I undertand that every method that shouldn't be called after the object's been disposed should throw the ObjectDisposedException. But what is the standard for the name object that should be passed to the exception's…

Wilhelm
- 1,868
- 14
- 21
25
votes
5 answers
Can a CryptoStream leave the base Stream open?
I create a MemoryStream, pass it to CryptoStream for writing. I want the CryptoStream to encrypt, and leave the MemoryStream open for me to then read into something else. But as soon as CryptoStream is disposed, it disposes of MemoryStream…

Edward Ned Harvey
- 6,525
- 5
- 36
- 45
19
votes
3 answers
What could be causing a "Cannot access a disposed object" error in WCF?
I am using the following code:
private WSHttpBinding ws;
private EndpointAddress Srv_Login_EndPoint;
private ChannelFactory Srv_LoginChannelFactory;
private Srv_Login.Srv_ILogin LoginService;
The Login is my…

Arian
- 12,793
- 66
- 176
- 300
13
votes
14 answers
Closing a form from the Load handler
I have a very strange behavior that only seems to happen on one form.
Basically I am creating an instance of a Form, and calling Show() to display the form non-blocking. In that form's Load event handler, I have some logic that may call…

LoveMeSomeCode
- 3,888
- 8
- 34
- 48
12
votes
2 answers
ObjectDisposedExecption after closing a .NET SerialPort
I am using a .NET 4 SerialPort object to talk to a device attached to COM1.
When I am done with the device, I call Close on the SerialPort. I do not call Dispose, but I believe that Close and Dispose are synonymous here.
Usually this works just…

DaleStan
- 595
- 2
- 6
- 19
11
votes
4 answers
ASP.Net Entity Framework, objectcontext error
I'm building a 4 layered ASP.Net web application.
The layers are:
Data Layer
Entity Layer
Business Layer
UI Layer
The entity layer has my data model classes and is built from my entity data model (edmx file) in the datalayer using T4 templates…

Chris Klepeis
- 9,783
- 16
- 83
- 149
11
votes
4 answers
How to prevent GraphicsDevice from being disposed when applying new settings?
My game window has manual resizing allowed, which means it can be resized like any other normal window, by dragging its edges. The game also makes use of a RenderTarget2D rt2d, to which the main Render Target is set in the main Draw method:…

user1306322
- 8,561
- 18
- 61
- 122
10
votes
3 answers
ObjectDisposedException when closing SerialPort in .Net 2.0
I have a C# windows forms application which communicates with a USB dongle via a COM port. I am using the SerialPort class in .Net 2.0 for communication, and the serial port object is open for the lifetime of the application. The application sends…

barry
- 4,037
- 6
- 41
- 68
9
votes
2 answers
Program crash with System.ObjectDisposedException
I'm having a weird problem with my program. I'm developing a windows form application. When I debug it through Visual Studio (F5) it works perfectly correctly, but when I run its executable it crashes and gives me "An unhandled exception of type…

armin
- 1,985
- 6
- 38
- 52
9
votes
2 answers
Who owns wrapped streams (e.g. TextWriter) in .NET?
I've recently encountered an error "ObjectDisposedException: Cannot access a closed Stream"
[ObjectDisposedException: Cannot access a closed Stream.]
System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +10184402
…

user2864740
- 60,010
- 15
- 145
- 220
7
votes
5 answers
When is it reasonable to check if this object has been disposed and throw ObjectDisposedException?
In a class that implements IDisposable, when is it reasonable to check if the object has been disposed and throw ObjectDisposedException if it has? In all public methods and properties (except Dispose)? Sometimes? Never?

Johann Gerell
- 24,991
- 10
- 72
- 122
6
votes
3 answers
C# IDisposable pattern and throwing ObjectDisposedException
A question for you all.
In my company, we are developing an application that runs inside Microsoft’s MVC framework.
The controller classes that we are implementing inherit from the MVC base class Controller. Example:
public class MyController:…

user3791684
- 61
- 3
6
votes
4 answers
IDisposable, ObjectDisposedException, and threadsafe types
Is there any point in keeping track of the classic bool disposed field on an otherwise threadsafe type for the purposes of conditionally throwing an ObjectDisposedException at the beginning of all primary exposed methods?
I've seen this pattern…

Jason Kleban
- 20,024
- 18
- 75
- 125
6
votes
2 answers
Entity framework - objectcontext disposed exception
I've been debugging this program without any result, and unfortunately I can't see the root of the problem. I get this exception: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
There…

Mitulát báti
- 2,086
- 5
- 23
- 37
6
votes
1 answer
Itextsharp HTMLWorker.Parse error
I have a problem with HTMLWorker.Parse From iTextSharp in a
Windows Form program. Everytime when I excecute the code and it
starts with the HTMLWorker.Parse, it gives the objectDisposedException.
The exception says that it cannot access a closed…

Emon
- 63
- 1
- 2
- 5