Dispose does not remove all the references at all, it is up to you to ensure
this happens. Dispose is used to clean up an objects resources, so that they
are not held open whilst awaiting a garbage collection. That's all. So
basically, you appear to have a reference somewhere to the disposed panel,
and your code is attempting to access it and use it. Said code appears to be
attempting an operation on the panel that requires the resources you've just
disposed to be available.
Dispose - Used to manually clear up resources when object is no longer
needed.
Finalize - Used to automatically clear up resources when object is garbage
collected.
The object will only be destroyed some arbitrary time after all references
to it are cleared.
From your stack trace, it looks like set_Active is trying to set focus to
the disposed panel.
HTH
http://codingsanity.blogspot.com
"Ryan Park" <ba**@intizen.com> wrote in message
news:50**************************@posting.google.c om...
Hi,
//SITUATION
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.
//ACTUAL CODE PRAGMENT & SYMPTOMS
//_body is a member panel control on the form control.
//And on this panel there is another panel control that holds other UI
controls.
if(_body.Controls.Count > 0)
{
_body.Controls[0].Dispose(); //always ther is only 1 child.
_body.Controls.Clear();
}
_body.Controls.Add(control);
Application.DoEvents();
This works well. But after some time ( a few seconds maybe), I got
"System.ObjectDisposedException: Cannot access a disposed object named
\"LoginForm\".
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.
//MY QUESTION
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. :)
Regards,
Ryan
//POST SCRIPT - WHOLE ERROR MESSAGE
"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
System.Windows.Forms.ContainerControl.FocusActiveC ontrolInternal()\r\n
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(M essage& m)\r\n
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at
System.Windows.Forms.UnsafeNativeMethods.PeekMessa ge(MSG& msg,
HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)\r\n at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMethods+IMsoComponentManager. FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at
System.Windows.Forms.ThreadContext.RunMessageLoopI nner(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"