TreeView gets disposed while still visible

G

Guest

I have a reproducible scenario here where I have a TreeView that is displayed
on a Form.

I have a custom class defined in the base which is nothing more than a fancy
BindingList<T> While building the node, I add event handlers on the
..ListChanged event with definitions to rebuild the necessary parts of the
TreeView should their collections change.

I modify these collections with forms that are created and made visible with
ShowDialog(this).

Occasionally (about 20-30% of the time) when I modify the collection from
this alternate form, an Exception fires while the TreeView is getting rebuilt
saying that it cannot access a Disposed object.

I added a Disposed event handler and placed a breakpoint there, as well as
in the overrides Diposed method in the Designer.cs class. Neither were hit.

What is being thrown here?? I can't seem to find any reason for this, and it
only happens on one of my two development machines.

Would appreciate any help I can get. Also can re-explain anything that came
across confusing.
 
B

Bojan Mijuskovic

If I understand correctly, you are saying that you manually created binding
to automate creation of tree nodes based on the collection?

In general, disposing in .NET is not something you can rely on - it depends
on the GC thread that decides on disposing the object and calculation is
based on various factors that's hard to control. Therefore the breakpoint
you have set might not fire when you expect. And yes - that can vary from
machine to machine.

But if the exception is still being thrown, you should maybe check any other
objects you expect to be alive and which have been garbage collected in the
meantime. By definition, object is scheduled for GC once there are no more
references to it, but that doesn't mean that it will be cleaned-up
immediately.

What about the modal form that you show? Is the collection you use after
closing the modal form instantiated within that form? It looks to me that
the problem is in your ListChanged event handler, where you access the
collection while rebuilding the tree node. Something like the collection is
disposed maybe? Although, it's hard to say more without seeing the code...

Try putting breakpoints in various places: when you build the tree node,
before creating the modal form, after closing it, etc.


Regards,
Bojan Mijuskovic [MVP]
 
G

Guest

See comments interjected below.

Bojan Mijuskovic said:
If I understand correctly, you are saying that you manually created binding
to automate creation of tree nodes based on the collection?
Yes, this is correct.
In general, disposing in .NET is not something you can rely on - it depends
on the GC thread that decides on disposing the object and calculation is
based on various factors that's hard to control. Therefore the breakpoint
you have set might not fire when you expect. And yes - that can vary from
machine to machine.
This is what doesn't make sense. The form is still visible. I can still see
it on my screen. I would expect the Disposed event would get fired if the
object was indeed Diposed. Also, I would expect a breakpoint in the "Dispose"
method would be caught as well.
What about the modal form that you show? Is the collection you use after
closing the modal form instantiated within that form? It looks to me that
the problem is in your ListChanged event handler, where you access the
collection while rebuilding the tree node. Something like the collection is
disposed maybe? Although, it's hard to say more without seeing the code...
There is only one collection. It is instantiated in the main form, and then
passed into the child form. The child form modifies this collection and then
closes. The main form updates automatically because of the ListChanged event.
Try putting breakpoints in various places: when you build the tree node,
before creating the modal form, after closing it, etc.
I do have the breakpoints in the Dispose method and the Disposed event, and
they get caught when i close the form normally, but not when this happens.

It is really weird. I just got the VS 2005 SP1 and I will see if i can
reproduce it under there, hopefully no, it is aggrivating.
 
B

Bojan Mijuskovic

This is what doesn't make sense. The form is still visible. I can still
see
it on my screen. I would expect the Disposed event would get fired if the
object was indeed Diposed. Also, I would expect a breakpoint in the
"Dispose"
method would be caught as well.

How are you so sure that the form is getting disposed? Have you seen the
stack of the thrown exception? There should be some line of the code where
the exception was thrown. Could you think of any other instance getting
disposed? Does any of the classes you use implement IDisposable interface?
There is only one collection. It is instantiated in the main form, and
then
passed into the child form. The child form modifies this collection and
then
closes. The main form updates automatically because of the ListChanged
event.

The BingingList<T> you are talking about is updated from the child form.
Did you try using ref keyword and see if there's any difference? What type
you use in this generic collection?
I do have the breakpoints in the Dispose method and the Disposed event,
and
they get caught when i close the form normally, but not when this happens.

Including the breakpoints in the child form, too?
It is really weird. I just got the VS 2005 SP1 and I will see if i can
reproduce it under there, hopefully no, it is aggrivating.

Yes, have in mind it's Beta though. Good luck!


Regards,
Bojan Mijuskovic [MVP]
 
G

Guest

This is what doesn't make sense. The form is still visible. I can still
How are you so sure that the form is getting disposed? Have you seen the
stack of the thrown exception? There should be some line of the code where
the exception was thrown. Could you think of any other instance getting
disposed? Does any of the classes you use implement IDisposable interface?
I know the form is disposed as when I try to re-do part of the TreeNodes, it
complains about the form being disposed. also TreeView.IsDisposed is true.
I don't implement IDisposable directly, it is always done by the form itself.
The BingingList<T> you are talking about is updated from the child form.
Did you try using ref keyword and see if there's any difference? What type
you use in this generic collection?
The BindingList is passed in through the use of a property, not a method.
the type is another custom class i have defined.
Including the breakpoints in the child form, too?
I ever thought that the child form could dispose of the parent forms
controls. The Child form has no reference to the TreeView on the main form,
just the collection.
Yes, have in mind it's Beta though. Good luck!
Ya, took ages to install, but seems to be working well.
 

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