none
Cannot access a disposed object.Object name: 'Icon'. RRS feed

  • Question

  •  

    Hi

     

    I got a problem while closing maximized MDI child form in my application....it is only when i set icon of the form if i dont set icon of the form it works fine......

     

    System.ObjectDisposedException : {"Cannot access a disposed object.\r\nObject name: 'Icon'."}Source:                                           "System.Drawing"

     

     

    do any one have any idea...plz help me.....

     

    Thanx in advance

     

    Regards

    Hammad

    Friday, August 3, 2007 6:22 AM

All replies

  • Hi Hammad,

     

    I cannot reproduce the problem, Could you please provide more information about this problem? Have you tried to access the icon when you have Closed the MdiChild form?

     

    Thanks.

     

    Monday, August 6, 2007 3:33 AM
  •  

    Hi Rong-Chun

     

    I m not trying to access icon on closing MDI Child form. the scenerio is when i set icon of the MDI Child form and maximized it..and in the maximized state when i tried to close, got this exception. In normal state it works fine...

     

    below you can find details of exception

     

    System.ObjectDisposedException : {"Cannot access a disposed object.\r\nObject name: 'Icon'."}Source:                                           "System.Drawing"

    StackTrace

    "   at System.Drawing.Icon.get_Handle()\r\n   at System.Drawing.Icon.get_Size()\r\n   at System.Drawing.Icon.ToBitmap()\r\n   at System.Windows.Forms.MdiControlStrip.GetTargetWindowIcon()\r\n   at System.Windows.Forms.MdiControlStrip..ctor(IWin32Window target)\r\n   at System.Windows.Forms.Form.UpdateMdiControlStrip(Boolean maximized)\r\n   at System.Windows.Forms.Form.WmSize(Message& m)\r\n   at System.Windows.Forms.Form.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.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.DefMDIChildProc(IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n   at System.Windows.Forms.Form.DefWndProc(Message& m)\r\n   at System.Windows.Forms.Control.WmWindowPosChanged(Message& m)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)\r\n   at System.Windows.Forms.ContainerControl.WndProc(Message& m)\r\n   at System.Windows.Forms.Form.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.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.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)\r\n   at System.Windows.Forms.Control.DefWndProc(Message& m)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.MdiClient.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.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.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)\r\n   at System.Windows.Forms.Control.DefWndProc(Message& m)\r\n   at System.Windows.Forms.Control.WmWindowPosChanged(Message& m)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.MdiClient.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.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.SafeNativeMethods.SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, Int32 x, Int32 y, Int32 cx, Int32 cy, Int32 flags)\r\n   at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)\r\n   at System.Windows.Forms.MdiClient.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)\r\n   at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.SetBounds(Rectangle bounds, BoundsSpecified specified)\r\n   at System.Windows.Forms.Layout.DefaultLayout.ApplyCachedBounds(IArrangedElement container)\r\n   at System.Windows.Forms.Layout.DefaultLayout.xLayout(IArrangedElement container, Boolean measureOnly, Size& preferredSize)\r\n   at System.Windows.Forms.Layout.DefaultLayout.LayoutCore(IArrangedElement container, LayoutEventArgs args)\r\n   at System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs)\r\n   at System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)\r\n   at System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent)\r\n   at System.Windows.Forms.Form.OnLayout(LayoutEventArgs levent)\r\n   at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)\r\n   at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)\r\n   at System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)\r\n   at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)\r\n   at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)\r\n   at System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)\r\n   at System.Windows.Forms.ToolStrip.DoLayoutIfHandleCreated(ToolStripItemEventArgs e)\r\n   at System.Windows.Forms.ToolStrip.OnItemVisibleChanged(ToolStripItemEventArgs e, Boolean performLayout)\r\n   at System.Windows.Forms.ToolStrip.OnItemRemoved(ToolStripItemEventArgs e)\r\n   at System.Windows.Forms.ToolStripItemCollection.OnAfterRemove(ToolStripItem item)\r\n   at System.Windows.Forms.ToolStripItemCollection.Remove(ToolStripItem value)\r\n   at System.Windows.Forms.ToolStripManager.RevertMergeInternal(ToolStrip targetToolStrip, ToolStrip sourceToolStrip, Boolean revertMDIControls)\r\n   at System.Windows.Forms.Form.UpdateMdiWindowListStrip()\r\n   at System.Windows.Forms.Form.OnVisibleChanged(EventArgs e)\r\n   at System.Windows.Forms.Control.WmShowWindow(Message& m)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)\r\n   at System.Windows.Forms.ContainerControl.WndProc(Message& m)\r\n   at System.Windows.Forms.Form.WmShowWindow(Message& m)\r\n   at System.Windows.Forms.Form.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.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.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)\r\n   at System.Windows.Forms.Control.DefWndProc(Message& m)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.MdiClient.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.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.DefMDIChildProc(IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n   at System.Windows.Forms.Control.DestroyHandle()\r\n   at System.Windows.Forms.Control.Dispose(Boolean disposing)\r\n   at System.Windows.Forms.Form.Dispose(Boolean disposing)\r\n   at YOUnique.Property.AddProperty.Dispose(Boolean disposing) in D:\\youniqe\\RealEstate\\RealEstate\\Views\\Property\\AddProperty.Designer.cs:line 20\r\n   at System.ComponentModel.Component.Dispose()\r\n   at System.Windows.Forms.Form.WmClose(Message& m)\r\n   at System.Windows.Forms.Form.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.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.DefMDIChildProc(IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n   at System.Windows.Forms.Form.DefWndProc(Message& m)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)\r\n   at System.Windows.Forms.ContainerControl.WndProc(Message& m)\r\n   at System.Windows.Forms.Form.WmSysCommand(Message& m)\r\n   at System.Windows.Forms.Form.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.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.DispatchMessageW(MSG& msg)\r\n   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.Run(Form mainForm)\r\n   at YOUnique.Program.Main() in D:\\youniqe\\RealEstate\\RealEstate\\Program.cs:line 21\r\n   at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)\r\n   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ThreadHelper.ThreadStart()"

    Monday, August 6, 2007 5:14 AM
  • Hi,

     

    Put this code in ur MDI Child FormClosing Event

    this.WindowState = FormWindowState.Normal;

     

    This would resolve ur issue.

     

    -Coumar.

     

    Friday, August 24, 2007 8:10 AM
  • Hello,

    I am coming back on this problem because I may have some interesting info.

     

    I have the exact same problem if I use icon which size is different than 16x16.

    I first tried with a 32x32 icon and i got that exception about the icon already disposed.

    After some test and comparison with previous applications I resize my icon and it is now working like a charm.

     

    my 2 cents..

     

    Thursday, April 10, 2008 12:20 PM
  • Hey we are using a 16x16 icon and still are seeing this issue, not every time, but maybe once a week to sometimes once a day. Anyone know of a real workaround for this crash issue. Setting a window on close to Normal state is not an option since it changes all windows to Normal state.

     

    Tuesday, April 29, 2008 7:58 PM
  • I'm seeing the same problem.  I'm using an MDI interface where each child form has a 32 bit icon.  The user maximized the window, left for awhile, then returned to the computer and closed the application with the MDI child maximized.  It seems that the framework is trying to perform garbage collection for the form's icon even though the form is open.  I agree that setting the window to Normal state on close is pretty kludgy... but I'll try it in the absence of any other solution.
    Saturday, May 31, 2008 2:44 AM
  •  

    I’m still having the same problem. I tried changing the size of the icons; I have an icon with all the possible sizes. 16X16  32X32 64X64, 72, 80, 96, 128…and .256X256 (for vista)

    But this problem only occurs when I load the application with Vista and 120DPI.

    The solution works very well. But the problem is that all the MDI childs go to Normal State when I close just one.

    protected override void OnClosing(CancelEventArgs e)

    {

    this.WindowState = FormWindowState.Normal;

    base.OnClosing(e);

    }

    Do u have any other idea or other solution??

    Monday, June 9, 2008 6:29 PM

  • Sorry, no good solution yet, but here's a little more info:  I haven't tested under Vista, yet.  The only time it's happened was with WinXP and a 32x32 icon.  I was planning to handle the OnClosing event as discussed, but changed my mind for the reason you mentioned.  I changed the icon to 16x16 and so far the bug hasn't resurfaced.  I'm basically waiting to see if it does.
    Monday, June 9, 2008 6:54 PM
  •  

    http://msdn.microsoft.com/en-us/library/aa511280.aspx

     

    In your case 96DPI and a scale factor of 100% Microsft says that the icons must be 16X16 or 32X32 according to that page. In my case under Vista 120DPI is not working yet....

    Monday, June 9, 2008 9:52 PM
  • System.InvalidOperationException: A fatal exception occurred: Cannot access a disposed object.

    Object name: 'Icon'. ---> System.ObjectDisposedException: Cannot access a disposed object.

    Object name: 'Icon'.

       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.WmSize(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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

       --- End of inner exception stack trace --

     

    Is it a Microsoft bug?? any other idea??

    Tuesday, June 10, 2008 3:02 PM
  • Hi,

    It's a bit of a hack but I found that setting the form's parent to null in the closing event seemed to resolve this problem. It flashes when you do this but doing a hide immediately before stops that.

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {

    this.Hide();
    this.Parent = null;
    }

    Thanks,
    Lukas
    • Proposed as answer by Codesleuth Thursday, February 25, 2010 10:30 AM
    Wednesday, June 11, 2008 8:45 AM
  • I too ran into this issue when using a 32x32 icon on a maximized child form.  If the icon was removed, it worked fine.  Put it back on, got the exception again any time I tried to close a maximized form.

    Lukas's solution works, but only the this.Hide() in the Form Closing event is necessary.  No need to set the parent to null.  This also preserves the window states of the rest of forms in the app.

    It's definitely just a workaround.  I'd say this issue is a Microsoft bug.
    Monday, July 7, 2008 1:47 PM
  • If your closing the form and opening the same for again and getting this exception, then overwrite the dispose method and unwire all the events in the form.

    this might work.

    thanks.
    chandra.
    Thursday, November 27, 2008 7:40 AM
  • Hi,

    It's a bit of a hack but I found that setting the form's parent to null in the closing event seemed to resolve this problem. It flashes when you do this but doing a hide immediately before stops that.

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {

    this.Hide();
    this.Parent = null;
    }

    Thanks,
    Lukas

    Proposed as the answer, as this is the best solution to the problem. It may not be the actual solution, but it's a great workaround and I'm happy to have my clients off my back about the problem.

    Software Developer, MCP
    Thursday, February 25, 2010 10:31 AM
  • If your closing the form and opening the same for again and getting this exception, then overwrite the dispose method and unwire all the events in the form.

    this might work.

    thanks.
    chandra.

    I just start to learn it, I learned a lot from what you said.
    Tuesday, September 14, 2010 2:18 AM
  • Thanks for the workaround.

    I observed a rare error after applying this workaround. It happened when I performed a call to "Application.Exit" (in our app: after any unhandled exception).

    The error is:

    System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
    at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
    at System.Windows.Forms.Application.ExitInternal()
    at System.Windows.Forms.Application.Exit(CancelEventArgs e)


    Workaround: don't perform the "Hide" workaround when the form is closed because of an "Application.Exit" call:

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
          base.OnFormClosing(e);
    
          if (e.CloseReason != CloseReason.ApplicationExitCall)
          {
            this.Hide();
          }
        }

    Best regards

    Wolfgang

    Monday, January 28, 2013 2:02 PM
  • Just doing the hide seemed to help me.  Though, we ended up doing on form close instead of form closing because we have a few forms that interrupt the close process to ask if the user wants to save.
    Wednesday, July 22, 2015 11:04 PM
  • Thank you Lukas and Wolfgang !!!

    this.Hide(); help me too. I wasted 2 days on that problem.


    Sven Kuehne (Rheotec)

    Thursday, September 27, 2018 1:43 PM