Problem (dispose?) MDI forms

G

Guest

Hi,

I've a very weird error in an MDI application. From time to time, when
closing a child form, the form is closed correctly (put some writelines on
different places: form_closing / form_closed / dispose) but is not removed
from the MDIparent.

When I iterate over all child forms I can close the form a second time (?)
even all controls are already disposed.

When clicking on the "closed" form in the MDI list, I get an ObjectDisposed
Exception.

A first chance exception of type 'System.ObjectDisposedException' occurred
in System.Windows.Forms.dll
System.ObjectDisposedException: Cannot access a disposed object.

Anyone who had similar problems ? Suggestions ?
 
V

VJ

Sure.. Close does not mean the object is immediately available to be garbage
collected and disposed. There is still other objects created by child Form2
in memory, as long as all of it are not cleared Form2 will exist. Now these
are not limited to Child form controls, they can be threads or any other
objects created when Form2 i.e. child was in memory and working...

HTH
VJ
 
G

Guest

I agree that closing a form doens't mean that the form is immediate released,
but the problem is a little more subtile: I'm sure that the form is disposed
(I put some code in the destructor), but it remains accessible through the
mdilist and in the midichildren property. As far as I know, no addtional
child are created inside the form (I additionaly even check if all controls
are removed...)

I agree, that it could take a while to be GCed (depending on memory
consumption / load and other things) but once a form is closed it shouldn't
be accessible anymore (In my situation is even worde bacause I'm using a
tabbed mdi interface and the users stills sees the "closed" window in the MDI
tab. Clicking on it generates the exception)...
 
V

VJ

Very tricky. I truly agree. Have developed MDI apps for last few years as
programmer. I don't like solutions like these, but sometimes after close
you can just set the object to null.

One thing tho'.. if you tabbed MDI, how is a child Form in a tab?, and you
do frm.Close().. and I am assuming you remove the Tab. Just curious on those
steps..!

VJ
 
G

Guest

I don't call the close myselft. It's done by the tabbedMDI interface from
Janus (www.janusys.com). It looks like a tab where eveyry form is represented
on a seperate tabpage.

I don't think it's related to them, because I use these controls in a number
of projects and it don't have similar problems. It looks like in this case
the notification to the mdiparent gets lost...
 
V

VJ

aah 3rd party control. You could post on their site or ask them see if there
are known issues that you should look for. You could use the debug
environment to see if all objects are cleaned up as promised.!

VJ
 
G

Guest

I already did this action before posting to the MSDN forurm. There are no
known issues...
 
V

VJ

I still some how believe some object related to the child is not cleaned up.
But again this may be not the case. You can have null checks and do a
specific null assignment to get around. Not a best solution, but avoids
exception.

Does the exception stack trace help?

VJ
 
G

Guest

Not really, It give some ecxception on the activation on one of the first
controls on
the form

A first chance exception of type 'System.ObjectDisposedException' occurred
in System.Windows.Forms.dll
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'MaskEdit'.
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.TextBoxBase.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.GetSafeHandle(IWin32Window window)
at System.Windows.Forms.ToolTip.Hide(IWin32Window win)
at System.Windows.Forms.ToolTip.HideAllToolTips()
at System.Windows.Forms.ToolTip.BaseFormDeactivate(Object sender,
EventArgs e)
at System.Windows.Forms.Form.OnDeactivate(EventArgs e)
at System.Windows.Forms.Form.set_Active(Boolean value)
at System.Windows.Forms.Form.DeactivateMdiChild()
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)
at System.Windows.Forms.UnsafeNativeMethods.DefMDIChildProc(IntPtr hWnd,
Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Form.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(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)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.MdiClient.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)
at Janus.Windows.UI.JNSZ.a(IntPtr , IntPtr , Int32 , IntPtr , IntPtr )
at Janus.Windows.UI.Internal.JNSAQ.WndProc(IntPtr hWnd, Int32 iMsg,
IntPtr wParam, IntPtr lParam)
at Janus.Windows.UI.Dock.Internal.JNSDA.WndProc(IntPtr hWnd, Int32 msg,
IntPtr wParam, IntPtr lParam)
at Janus.Windows.UI.Internal.JNSAQ.a(IntPtr , Int32 , IntPtr , IntPtr )
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd,
Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam,
Int32 lparam)
at System.Windows.Forms.Form.Activate()
at Janus.Windows.UI.Dock.UIMdiTabGroup.c(JNSBG )
at Janus.Windows.UI.Dock.UIMdiTabGroup.OnMouseDown(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.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)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at CCREK.Midas.GUI.Classes.Launcher.Main(String[] args) in
 
V

VJ

Hello,

Using third party controls is always tricky, You don't have source code that
I can run to replicate the problem. I understand that is going to be tricky
in this situation as you use third party tools and controls. The key for you
should be to make sure that not get caught up on the broken piece. Examine
other code around, that might lead to it. Objects left un disposed can cause
a Pain. There is a tool called FxCop ( Microsoft tool) that helps identify
some issue with design and code flow. You could also get a trial version of
memory profiler and see if that spots the issue. Its time to stretch out the
normal means and try to solve the problem.

I am trying to give pointers here to you, as I don't have your full code or
sample that can re-produce the problem. Please do spend time eliminating
options, upto now we have done a lot of elimination and that is not enough
for your problem. Also if you can ask somebody from your team to look at it,
always a second pair of eyes helps.

Hope you solve the problem, let me know if we here can help you in any other
way.
VJ

SealedClassSingleton said:
Not really, It give some ecxception on the activation on one of the first
controls on
the form

A first chance exception of type 'System.ObjectDisposedException' occurred
in System.Windows.Forms.dll
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'MaskEdit'.
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.TextBoxBase.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.GetSafeHandle(IWin32Window window)
at System.Windows.Forms.ToolTip.Hide(IWin32Window win)
at System.Windows.Forms.ToolTip.HideAllToolTips()
at System.Windows.Forms.ToolTip.BaseFormDeactivate(Object sender,
EventArgs e)
at System.Windows.Forms.Form.OnDeactivate(EventArgs e)
at System.Windows.Forms.Form.set_Active(Boolean value)
at System.Windows.Forms.Form.DeactivateMdiChild()
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)
at System.Windows.Forms.UnsafeNativeMethods.DefMDIChildProc(IntPtr hWnd,
Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Form.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(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)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.MdiClient.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)
at Janus.Windows.UI.JNSZ.a(IntPtr , IntPtr , Int32 , IntPtr , IntPtr )
at Janus.Windows.UI.Internal.JNSAQ.WndProc(IntPtr hWnd, Int32 iMsg,
IntPtr wParam, IntPtr lParam)
at Janus.Windows.UI.Dock.Internal.JNSDA.WndProc(IntPtr hWnd, Int32 msg,
IntPtr wParam, IntPtr lParam)
at Janus.Windows.UI.Internal.JNSAQ.a(IntPtr , Int32 , IntPtr , IntPtr )
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd,
Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam,
Int32 lparam)
at System.Windows.Forms.Form.Activate()
at Janus.Windows.UI.Dock.UIMdiTabGroup.c(JNSBG )
at Janus.Windows.UI.Dock.UIMdiTabGroup.OnMouseDown(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.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)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at CCREK.Midas.GUI.Classes.Launcher.Main(String[] args) in


VJ said:
I still some how believe some object related to the child is not cleaned
up.
But again this may be not the case. You can have null checks and do a
specific null assignment to get around. Not a best solution, but avoids
exception.

Does the exception stack trace help?

VJ
"SealedClassSingleton" <[email protected]>
wrote in message
 
V

VJ

I shall give you a typical example we went through with a 3rd party menu /
toolbar tool that we got when we where with .NET 1.1. They offered nice
toolbar menus and merging for a MDI, but it took a while to realize they
where using threads to merge of Toolbars.. turned to be a nightmare than
what we imagined. I am not saying this is all 3rd party tools and companies
this will happen. Just that we need to be well aware we work with a binaries
and not source files to identify problems easily, just like working .NET
framework. Also these situations most of the problems are erratic and never
consistent. So be patient and go outside norms to solve the problem. I think
you are just focused on just one point, that might not be the case here..

VJ
 
L

Linda Liu [MSFT]

Hi Eagle,

Based on my understanding, you have an MDI application and the MDI parent
has a menu for its MDI children list. The problem is that when you close an
MDI child form, it is still available in the MDI children list within the
menu and in the MdiChildren property of the MDI parent. If you click on the
'closed' form, you get an ObjectDisposedException. If I'm off base, please
feel free to let me know.

I have performed a test in both VS.NET 2003 and VS 2005, but couldn't
reproduce the problem on either of them.

In my test VS.NET 2003 project, I add a Menu to the MDI parent form, and
add a MenuItem in the Menu. I set the MdiList property of the MenuItem to
True.

When the program is run and I open several MDI child forms, all the MDI
child forms are listed under the MenuItem. When I close one of the MDI
child forms, it disappears from the MDI child list and doesn't exist in the
MdiChildren property of the MDI parent, either.

In my test VS 2005 project, I add a MenuStrip on the MDI parent form and
add a ToolStripMenuItem to the MenuStrip. I set the MdiWindowListItem
property of the MenuStrip to the ToolStripMenuItem. When the program is
run, the program behaves the same as my VS.NET 2003 test program does.

Does this problem always exist when you close the MDI child form?

Since I couldn't reproduce the problem on my side, it's hard for me to
troubleshoot the problem. If possible, could you please send me a sample
project that could just reproduce the problem? To get my actual email
address, remove 'online' from my displayed email address.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi,

Your description og the problem is right. The problem doesn't always occurs.
I cann't force it to happen and I cann't easily isolate part of the project
and send it over.

regards,

Bart.
 
L

Linda Liu [MSFT]

Hi Bart,

Thank you for your reply.

What the VS IDE are you using, VS.NET 2003 or VS 2005?

Could you please tell me what controls are on the MDI child form? From the
call stack you have provided, it seems that MDI child form has ToolTip
component on it. If so, the problem may be caused by the form's being
incorrectly disposed during form closing.

As VJ has suggested, you may dispose all the controls/unmanaged objects, if
any, on the MDI child form explicitly from within the form's override
Dispose(bool) method. You may also set the MDI child form's MdiParent
property to null in the form's Closed event handler.

Please try my suggestion and let me know the result.

If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top