Link to home
Start Free TrialLog in
Avatar of mrmox
mrmox

asked on

Array of WinForms: do I need to clean up after close before I re-use?

My project is using C# with VS2010 and .Net 4

I have a group of WinForms I am managing in an array.

        // This array holds the winforms that each contain some controls
        PopUpForm[] arrPopupWinFormInSrcOrder = new PopUpForm[MAX_WINFORMS];

Open in new window

When the array is created, all the entries are null, so I can test
if a particular WinForm has been created yet by testing for equal to null.

        if (PopUpForm[3] == null) { // OK to create

                arrPopupWinFormInSrcOrder[3] = new PopUpForm();

        }

Open in new window

Sometimes I need to re-create a WinForm in the same array position after
the user has closed it with the X in the upper right corner.  However,
in this case, the array entry is NOT null, since the WinForm had existed
at one time but was closed.

1. What should I be testing to know if the WinForm was closed by the user?

2. Is it the IsDisposed?

3. Is it safe to re-use the same array location for the new re-created WinForm,
or do I have to do any cleaning up functions first?

4. What would happen if I tried to assign null to an array location if I know
the user had closed that particular WinForm?

        PopUpForm[3] = null;

Open in new window

Complete answers would be especially appreciated, explaining well rather
then just some code without telling me what it is supposed to do.

And answering explicitly *each* of my numbered questions above.


Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrmox
mrmox

ASKER

I believe the answer was complete and correct, and might only have been improved by a link or two to some supporting authoritative sources.

IN THE END, my problem (bug) was this:  although C# does not generally destroy objects until all references to them are destroyed, and the (excellent) third-party graphing package I was using was also written in C#, something within DirectX that the third-party package had to use required that when a chart object was destroyed, all the objects referenced had to be destroyed.

I had pointed two chart objects  at the same Font via reference, and deleting one of them deleted the font, in spite of the fact that another chart object was still using that font.

SO I needed to learn that for this package, I should not share any objects between its chart objects.

If anyone sees something wrong with what I've said above, please speak up (but I am not sure how since the answer was accepted).

Thanks!
That's a nasty bug...was probably pretty hard to figure out!   =\