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!
mrmoxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
1. What should I be testing to know if the WinForm was closed by the user?
2. Is it the IsDisposed?

>> Correct.  IsDisposed() will be true if the Form was closed.

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?

>> Assuming you have no un-managed resources in the forms that require special disposition, then No.  The disposed forms will automatically be garbage collected for you.  It's fine to re-use the same array locations for the new forms.

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

>> Nothing.  The array will simply have a null stored in that location.  If there are no more references to that form then will eventually be garbage collected automatically.

Furthermore, if the Form is NOT closed and you assigned null to the Array where the form reference was located, NOTHING would also occur.  This is because the Form would still have a reference in the Application.OpenForms() collection.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrmoxAuthor Commented:
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!
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
That's a nasty bug...was probably pretty hard to figure out!   =\
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.