Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) 'System.ObjectDisposedException' (Read 9159 times)
Mark_Wills
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 27
Joined: Mar 6th, 2006
'System.ObjectDisposedException'
Aug 15th, 2007 at 11:42am
Print Post  
Hi,

I think I may have come across a bug in the FlowChart control, BUT, it might be me!

My app is an MDI application. My parent form creates an instance of the form that contains the flow chart (frmCanvas). When a user closes the frmCanvas form, it first sends an event to the parent form, to allow it disable toolbar buttons etc, and closes.

If you then open a new instance of the frmCanvas form (which contains the flowchart control) and then use the ZoomFactor property, you get a

[code]A first chance exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll
[/code] on the call to ZoomFactor.

Here is the line of code that generates the error:

[code]
FlowChart1.ZoomFactor = 200 - (_parentForm.cbZoom.SelectedIndex * 10)
[/code]

I have checked, and all variables/objects ARE loaded with real data, they are not 'Nothing' un-initialised etc... FlowChart1 DOES contain an instance of FlowChart. (FlowChart1 sits in the component tray at design time, under the form)

Looks like a prob in the ZoomFactor method... Any ideas?

Regards

Mark.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: 'System.ObjectDisposedException'
Reply #1 - Aug 15th, 2007 at 12:04pm
Print Post  
Hi,

That will happen if the OS handle of the flowchart window has been destroyed. If you are handling the form's Closed event, try moving the code from its handler to a Closing event handler.

Stoyan
  
Back to top
 
IP Logged
 
Mark_Wills
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 27
Joined: Mar 6th, 2006
Re: 'System.ObjectDisposedException'
Reply #2 - Aug 15th, 2007 at 12:37pm
Print Post  
Hi Stoyan,

Thanks for the suggestion, but sadly, no luck...

Here's the code I was using...

[code]

    Private Sub frmCanvas_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
       RaiseEvent IAmClosing() ' tell the parent form that this form is closing
       RemoveHandler _parentForm.LoadProject, AddressOf Me.LoadProject ' remove handler
       RemoveHandler _parentForm.SaveProject, AddressOf Me.SaveProject ' remove handler
       _parentForm.lblStatus.Text = "Project closed"   ' Text for parent forms Status bar (at bottom of form)
       _parentForm.ProjectInMemory(False)  ' flag to tell parent form that there is no project in memory
       FlowChart1.Dispose()    ' kill the flowchart object on this form
    End Sub
[/code]

Any other ideas?

Regards

Mark.
  
Back to top
 
IP Logged
 
Mark_Wills
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 27
Joined: Mar 6th, 2006
Re: 'System.ObjectDisposedException'
Reply #3 - Aug 15th, 2007 at 12:43pm
Print Post  
>That will happen if the OS handle of the flowchart window has been destroyed.

Hi Stoyan,

The handle to the flowchart object hasn't been destroyed, because I can draw on it!

Sequence of events goes like this:

1-Open a (child) window with Flowchart on it. Draw on it. No problem. Change zoom etc - no problem
2-Close window
3-Open a new instance of child window - draw on flowchart
4-Use combo box on parent form to change zoom *BANG*

Mark

  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: 'System.ObjectDisposedException'
Reply #4 - Aug 15th, 2007 at 1:02pm
Print Post  
Quote:
4-Use combo box on parent form to change zoom *BANG*


Are there any event handlers in the child form attached to events of the parent form UI? If so, try detaching them (RemoveHandler keyword in VB.NET) when a child form is closed. Otherwise the .NET form object might still be live even if the OS window is destroyed. Event handler invoked on such a closed form will lead to that exception.

Stoyan
  
Back to top
 
IP Logged
 
Mark_Wills
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 27
Joined: Mar 6th, 2006
Re: 'System.ObjectDisposedException'
Reply #5 - Aug 15th, 2007 at 1:20pm
Print Post  
Hi Stoyan,

THere are two event handlers, which are removes (as you can see from the above code).

I use MsgBox to see if the Zoom event is called on the child form, and it is. Here is the code...

Code
Select All
    Private Sub Zoom(ByVal SelectedIndex As Integer) Handles _parentForm.ZoomChanged
	  MsgBox("I am in frmCanvas.Zoom")
	  FlowChart1.ZoomFactor = 200 - (_parentForm.cbZoom.SelectedIndex * 10)
    End Sub
 



What happens is, when the user uses the drop down combo to change the zoom factor, this raises an event which is captured by frmCanvas (the form with the FlowChart).

I get the MsgBox correctly, and then *BANG*

I can also trace flow with single stepping and I can see that the right code is being called. It only crashes when there is a reference to ZoomFactor! This has got me stumped!
  
Back to top
 
IP Logged
 
Mark_Wills
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 27
Joined: Mar 6th, 2006
Re: 'System.ObjectDisposedException'
Reply #6 - Aug 15th, 2007 at 1:25pm
Print Post  
I think I should also say that there are other combo boxes on the parent form to do other things, such as set line types, and arrow head sizes. These work in EXACTLY the same was as Zoom: i.e. they raise an event which is captured by frmCanvas and processed. These work fine. ONLY the ZoomFactor is a problem. This is why I suspect it may be a problem with the FlowChart DLL.

Hope this info is of use.

Mark.
  
Back to top
 
IP Logged
 
Mark_Wills
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 27
Joined: Mar 6th, 2006
Re: 'System.ObjectDisposedException'
Reply #7 - Aug 15th, 2007 at 1:29pm
Print Post  
Here's an example of code that works fine: This is on the frmCanvas form (form with the flowchart). This is the event handler for changing the size of arrow heads on lines. It is invoked by a combo box on the parent form, and it works perfectly:

Code
Select All
    Private Sub ArrowSize(ByVal SelectedIndex As Integer) Handles _parentForm.ArrowSizeSelection

  defaultArrowHeadSize = _parentForm.cbArrowSize.SelectedIndex + 1


  Dim _arrow As MindFusion.Diagramming.WinForms.Arrow

  For Each _arrow In FlowChart1.Arrows


If _arrow.Selected Then _arrow.ArrowHeadSize = Me.defaultArrowHeadSize

  Next
    End Sub
 



If I add the following:
Code
Select All
    Private Sub ArrowSize(ByVal SelectedIndex As Integer) Handles _parentForm.ArrowSizeSelection

  defaultArrowHeadSize = _parentForm.cbArrowSize.SelectedIndex + 1


  Dim _arrow As MindFusion.Diagramming.WinForms.Arrow

  For Each _arrow In FlowChart1.Arrows


If _arrow.Selected Then _arrow.ArrowHeadSize = Me.defaultArrowHeadSize

  Next

  FlowChart1.ZoomFactor = 50 <<<<---- *BANG*
    End Sub
 



Hope this helps.

Mark.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: 'System.ObjectDisposedException'
Reply #8 - Aug 15th, 2007 at 4:45pm
Print Post  
Are you removing all event handlers, including the ones for the zoom combo box? Otherwise your child forms will never be freed by the Garbage Collector, since having an attached event is a valid reference to an object.

Just to test this, remove the code that throws an exception from the zoom combo handler (i.e. setting the ZoomFactor) and add some counter variable there. Then open and close child forms several times, leaving just one on the screen in the end. Now change the zoom level from the combo box - if everything is ok, the counter should increment just once. If the closed forms are still referenced through an event handler, the counter will increment more than once.

Getting an exception from the ZoomFactor setter, but not from other properties, might be related to the fact that ZoomFactor sets some properties of the flowchart's scrollbars. The scrollbars are child controls themselves and their window handles might be already destroyed.

Stoyan
  
Back to top
 
IP Logged
 
Mark_Wills
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 27
Joined: Mar 6th, 2006
Re: 'System.ObjectDisposedException'
Reply #9 - Aug 15th, 2007 at 7:26pm
Print Post  
Hi Stoyan, thanks for staying with me!

Yes, that's an excellent suggestion, I will try that tomorrow.

For what it's worth, I'm with you: I think it's a problem with my code, not the FlowChart control Smiley It's just being a pig to find Undecided

Thanks again,

Mark
  
Back to top
 
IP Logged
 
Mark_Wills
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 27
Joined: Mar 6th, 2006
Re: 'System.ObjectDisposedException'
Reply #10 - Aug 31st, 2007 at 9:44am
Print Post  
Stoyan,

I just wanted to post back and say thank you for your help. It was much appreciated.

The problem was with MY code, and NOT with the FlowChart.Net component, which works fine.

The problem was simple (they always are, eh?):

My main (MDI parent) form creates a child form which houses the FlowChart

The child form also creates a reference to the main form, so that methods etc on the main form can be called from the child form.

When the child form closed, it correctly removed the event handlers, but I forgot to change the main form reference to nothing.

DOH!

This was a horrible bug to find! The problem is that I am working on this program in my spare time and I sometimes struggle to remember what does what, and forget where to find things!

Anyway, that problem is fixed now.

THANKS FOR YOUR HELP, Stoyan  Grin
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: 'System.ObjectDisposedException'
Reply #11 - Aug 31st, 2007 at 11:39am
Print Post  
8)
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint