2
votes

I generally understand exception Cannot access a disposed object. But this time it seems to be originating in non-user code. I'm not posting my code because this time I don't know which one :) Crash occurrs occassionally when a form is closed. It looks like some automatic UI refresh is ongoing:

Message: Cannot access a disposed object.
Object name: 'Icon'.
HResult: -2146232798
HelpLink: (null)
Source: System.Drawing
TargetSite: IntPtr get_Handle()
StackTrace: 
       at System.Drawing.Icon.get_Handle()
       at System.Drawing.Icon.get_Size()
       at System.Drawing.Icon.ToBitmap()
       at System.Windows.Forms.MdiControlStrip.GetTargetWindowIcon()
       at System.Windows.Forms.MdiControlStrip..ctor(IWin32Window target)
       at System.Windows.Forms.Form.UpdateMdiControlStrip(Boolean maximized)
       at System.Windows.Forms.Form.UpdateToolStrip()
       at System.Windows.Forms.Form.OnMdiChildActivate(EventArgs e)
       at System.Windows.Forms.Form.ActivateMdiChildInternal(Form form)
       at System.Windows.Forms.Form.WmMdiActivate(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       --end of stack trace--

It is possible this one is thrown only debugger session, I haven't seen it in production so far. (See the bottommost item of the stack trace.)

  • Is my assumption (that this is caused only by debugging session) correct? Or should I worry about the same problem in production?

  • Can this problem be somehow avoided?

1
Any event handlers still attached? Seems a memory leak to me. Those event handlers try to access a disposed object.Patrick Hofman
@PatrickHofman – thank you for idea where to look. I will check.miroxlav
Yeah, something is attempting to call a referenced item that is out of scope already.Greg
Hmya, there are things you can do wrong in your own code that can, later, cause .NET Framework code to crash. This stack trace says it is trying to activate a window that isn't there anymore. That's not supposed to happen of course, to put it mildly. Using threads inappropriately is a pretty good way to induces crashes like this. And to incorrectly assume that the bug isn't real.Hans Passant
Hmm, no, never trust anything you see at the MSDN forums. Setting the Parent to null in the FormClosing event is grossly inappropriate, it recreates the window while it is busy closing. The kind of "thing you can do wrong" that causes crashes later. There is always a good reason for code the bomb but if you don't post a repro then nobody can tell you and every wild-ass guess looks correct.Hans Passant

1 Answers

4
votes

The call-stack you provided can be related to known .Net issue. See the following thread at MSDN-forum.
The possible solution is using Me.Hide in the FormClosing event handler for child form.