WinForms Cleanup - Object Disposed Exception

dotnet framework


  • 1. Read manifest data (GetReferencedAssemblies)
    Hello, is there any way to read all the assemblies and versions an assembly refers to? I am quite sure there must be a way, because ildasm shows all the references in paragraphs like this: .assembly extern retargetable System.Windows.Forms { .ver 1:0:5000:0 } This kind of method would be helpful creating about dialogs and to guarantee the integrity of a software installation. The full framework has methods like GetReferencedAssemblies, but in the .Net CF they are missing. Does anyone have an idea how to implement this myself and read an asemblie's manifest? Any idea is appreciated, Marc
  • 2. how do I add an icon to my app?
    Do I need a particular graphic format for this? Or can I use any ICO image. I tried using a windows icon and compiling my app. with this icon. It shows fine in my PC, but when I copy the executable to my pocket PC, I don't see the icon, but a general applkication icon. What am I doing wrong?
  • 3. Howto detect Power down/up notification?
    Hi, Is there a way to detect power up or power down notifications using Compact Framework ? Regards, NTN
  • 4. App worked Yesterday - today cursor just spins?!?!
    Hello, my project was working yesterday when I closed up shop. Today, I turned on Visual studio and before making any changes, I compiled a copy of my app in debug mode. All of the components copied to the emulator - but the app wouldn't launch. I waited 5-10 minutes, and still it wouldn't launch. So I hard-reset the emulator. Tried debugging again, and all the components copied over again, but the app never launched! So I rebooted my machine, put the ipaq in the cradle, and tried debugging from there. On the PPC, the cursor just kept spinning for about 60 seconds then went away. I verified that .NET apps were working by launching another .NET app on the pocket pc. It ran just fine. I coped an old frmmain.vb backup to the compile directory and it worked just fine, which makes me think it's something in the "Windows generated code" that is messing things up. Is there an easy way to find out what it is? My backup is from last week, so I'll lose 10 hours if I have to redo. Thanks, B.
  • 5. SQL Server CE and C#
    Hi everyone, I keep getting the following errors everytime i run Pull method in C# - ( SQL Server CE encountered problems in opening the SQL Server CE database. [,,,Database name,,]" ) - (An internal error occured. [ID,,,,,]) - Class not registered Any idea ?? Thanks Kam

WinForms Cleanup - Object Disposed Exception

Postby Jeff M » Sun, 10 Aug 2003 07:02:13 GMT

I have a forms class that contains as a member object another forms object.
The problem I am having is when the created forms object is closed, an
ObjectDisposedException exception is generated when I return to the main
form and execute the event handler the second time.

public class CustomerRequest_CSS056 : System.Windows.Forms.Form


private CustomerRequestInfo_CSS057 _customerRequestInfo;

private void MenuItemRequest_Click(object sender, System.EventArgs e)


if (this._customerRequestInfo == null)


this._customerRequestInfo = new





If I create the form on the stack and not as a member variable then the
exception goes away, but what is really going on here ?

When I close the created window using the <OK> button what actually happens
? Does that form get Disposed.

Suppose I just go about creating new Form() objects and calling Show() won't
I just have dozens of windows about ? Will the Garbage Collector clean this
up if I just keep constructing windows and call Show() ?

Thanks for the help

Similar Threads:

1.doing cleanup in Dispose() when implementing IDisposable

If I implement IDisposable in a custom class, how do I let the GC know
that the custom class objects aren't being used anymore, and are ready
for cleanup?

Do I just set them equal to null? Do I need to call GC.Collect() after?

Is IDisposable only meant for objects that already implement it like
sockets and stream?


2."Cannot access a disposed object" - weird exception.


I got a panel control that hold a certain position on a form. 
Every controls or UIs are on this panel.

At certain situation, I called dispose() method of this panel control
and change it with other panel which contains other business logic and
UI controls.

//_body is a member panel control on the form control.
//And on this panel there is another panel control that holds other UI

 if(_body.Controls.Count > 0)
   _body.Controls[0].Dispose(); //always ther is only 1 child.

This works well. But after some time ( a few seconds maybe), I got
"System.ObjectDisposedException: Cannot access a disposed object named

LoginForm is a control type. It's not a name of control.
And this LoginForm is on the _body.Controls[0] panel. 
And LoginForm.Dispose() get called when _body.Controls[0].Dispose();
line executes.

I have no idea how this happen. Since I call dispose(), I think there
should be no reference to panel instance.

I've tried GC.SuppressFinalize(), but not works either.

How can I fix this code? Or Is this a bug of .NET?

Any suggestion would be appreciated. TIA. :)


"System.ObjectDisposedException: Cannot access a disposed object named
\"LoginForm\".\r\nObject name: \"LoginForm\".\r\n   at
System.Windows.Forms.Control.CreateHandle()\r\n   at
System.Windows.Forms.Control.get_Handle()\r\n   at
  at System.Windows.Forms.Form.set_Active(Boolean value)\r\n   at
System.Windows.Forms.Form.WmActivate(Message& m)\r\n   at
System.Windows.Forms.Form.WndProc(Message& m)\r\n   at
System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)\r\n  
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)\r\n  
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)\r\n   at
System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg,
HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)\r\n   at
dwComponentID, Int32 reason, Int32 pvLoopData)\r\n   at
System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)\r\n   at
System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)\r\n   at
System.Windows.Forms.Application.Run(Form mainForm)\r\n   at
WindowsApplication1.Form1.Main() in
c:\\src\\smart\\windowsapplication1\\form1.cs:line 88" object disposed exception

I have a VS2003 VB.Net that is using sockets to exchange information 
with a server application.  Intermittently the application is receiving 
a object disposed exception that is not being 

My code looks like:


	sck. Send


Catch ex as Exception
	Some logging
end try



Catch ex as Exception
	Some logging
end try

The error is very intermittent but is not trapped by the code above.  Am 
I wrong in expecting that the Exception would be trapped using the 
Exception object?  Should I be using sockect specific exception types.

The application is also doing some database access to the server using 
rda.  Could the object disposed exception be 
thrown and trapped there?



4.Object disposed exception


I have a form with a control on it. I close the form in 
the deactivate event handler. I also close the form in 
control double-click event. In the form deactivate event 
handler I ensure that the form is not closed twice using 
my own flag.
In debug the form code seems to have the correct flow 
(Dispose() method is called only once), but after my code 
exits an exception is raised telling that my disposed 
control is accessed.
What may be the possible reasons for that?

Thanks in advance,

5.Thread Exception disposes Form Object

Dear All,
   I am creating my startup Form in a new thread the following way...
Thread oFormThread = new Thread(new ThreadStart(AddFormInThread));
 Application.ThreadException += new
  oHiddenForm = new frmStartup(this);
  oHiddenForm.Visible = false;
  oHiddenForm.IsAccessible = false;					

now if I create a Form Object x via reflection and throw an exception
in its constructor...I get the exception in
onFormThreadException...but the next time I again try to create the
Object x...I get the following exception...
Object reference not set to an instance of an object.
Stack Trace:    at System.Windows.Forms.SafeNativeMethods.MessageBox(HandleRef
hWnd, String text, String caption, Int32 type)
   at System.Windows.Forms.MessageBox.ShowCore(IWin32Window owner,
String text, String caption, MessageBoxButtons buttons, MessageBoxIcon
icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions
   at System.Windows.Forms.MessageBox.Show(String text)

I found out that my Startup is getting I doing something
wrong here...please help...

Many regards

6. "Can not access a disposed object" exception

7. Exception when Form object is accessed after being disposed

8. Object Disposed Exception Was Unhandled, Safe handle has been clos

Return to dotnet framework


Who is online

Users browsing this forum: No registered users and 75 guest