Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress/catch System.ObjectDisposedException?

Tags:

c#

.net

I have an application that sporadically throws this exception:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: "Panel". 
   bei System.Windows.Forms.Control.CreateHandle() 
   bei System.Windows.Forms.Control.get_Handle() 
   bei System.Windows.Forms.ContainerControl.FocusActiveControlInternal() 
   bei System.Windows.Forms.Form.set_Active(Boolean value) 
   bei System.Windows.Forms.Form.WmActivate(Message& m) 
   bei System.Windows.Forms.Form.WndProc(Message& m) 
   bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
   bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
   bei System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Is there a way to suppress this exception ideally without touching the code? I am thinking about some registry magic or esoteric .NET configs.

Furthermore I am of course also interested in ways to catch this exception. There seems to be no hook for me to catch this exception... And of course it is not reproducible...

like image 536
tatt Avatar asked Dec 07 '22 05:12

tatt


2 Answers

The runtime is trying to tell you something. Don't ignore it! Catching and ignoring the exception does not make the problem go away.

The specific exception tells you that you're trying to use a Panel after it has been disposed. So you basically have two options here: 1) Don't expose until you're finished using it. 2) Don't use it after it has been disposed.

EDIT: To help you troubleshoot the problem, you could set up ADPlus to create dump files for that specific exception. That could give you some insight as to why this happens. John Robbins has a Bugslayer article on how to do that. Please see http://msdn.microsoft.com/en-us/magazine/cc163530.aspx.

like image 178
Brian Rasmussen Avatar answered Jan 05 '23 09:01

Brian Rasmussen


"Is there a way to fix the pain in my left leg that I get every time I shoot myself in the foot? Perhaps taking copious amounts of paracetamol or similar?"

The answer here is not to fix the pain, it's avoiding shooting yourself in the foot.

In this case, there's code accessing a panel that is disposed. This code must be fixed, the answer is not to suppress the exception because you really do have a bug in your code. It's not the runtime that is in fault here.

Now, I see from comments that you want a "quick fix" to get back to work, that kind of attitude is not going to help you for long because, as you see, every answer and possible solution brings new questions.

So instead of spending time trying to silence the runtime telling you that you have a bug, stop doing that and fix the bug.

like image 21
Lasse V. Karlsen Avatar answered Jan 05 '23 09:01

Lasse V. Karlsen