0

I'm using form singleton that is created when needed:

    public PicForm dspForm;

    public PicForm getPicForm()
    {
        if (dspForm == null)
        {
            dspForm = new PicForm();
            dspForm.Visible = true;
        }
        dspForm.Show();
        return dspForm;
    }

When I close dspForm it not sets to null. Why? Since dspForm is not null how to know it is closed?

vico
  • 17,051
  • 45
  • 159
  • 315
  • 4
    Why should it be set to null if you don't do so explicitly? – Panagiotis Kanavos Dec 17 '18 at 11:00
  • 1
    Why do you expect it to be `null` after closing it? There´s nothing in the docs that would explain this. Anyway when closing a form its `Dispose`-method is called which will dispose any unmanaged resources. Checking for `null` has nothing to do with disposing a ressource. – MakePeaceGreatAgain Dec 17 '18 at 11:00
  • Is it invisible? The Show() method does not return from form until the form exits. See my two form project : https://stackoverflow.com/questions/34975508/reach-control-from-another-page-asp-net – jdweng Dec 17 '18 at 11:01
  • 1
    Unless you set it explicitely to `null`, `dspForm` won´t be null. However it may be **disposed**, which is what you should care for instead. – MakePeaceGreatAgain Dec 17 '18 at 11:02
  • Possible duplicate of [Detect when a form has been closed c#](https://stackoverflow.com/questions/8750602/detect-when-a-form-has-been-closed-c-sharp) – Falco Alexander Dec 17 '18 at 11:07
  • "how to know it is closed" Why do you want to know this? Your form is closed when someone hits the close-button (if existing) or the cross in the upper right. – MakePeaceGreatAgain Dec 17 '18 at 11:51
  • dspForm.FormClosed += delegate { dspForm = null; }; – Hans Passant Dec 17 '18 at 12:20

4 Answers4

0

The PicForm does not know anything about your variable here. You have to set variable to null (e.x. on FormClosed event).

    public PicForm getPicForm()
    {
        if (dspForm == null)
        {
            dspForm = new PicForm();
            dspForm.FormClosed += PicForm_FormClosed;
            dspForm.Visible = true;
        }
        dspForm.Show();
        return dspForm;
    }

    private void PicForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        PicForm picForm =(PicForm)sender;
        picForm.FormClosed += PicForm_FormClosed;
        dspForm = null;
    }

If you do not want to use additional events you could check if form was disposed (it will be if you close it ant it was shown with Show() method)

    public PicForm getPicForm()
    {
        if (dspForm == null || dspForm.Disposing || dspForm.IsDisposed)
        {
            dspForm = new PicForm();
            dspForm.Visible = true;
        }
        dspForm.Show();
        return dspForm;
    }

With this solution if you use dspForm somewhere else you also need to check if it was disposed. Otherwise you can get ObjectDisposedException

Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22
0

When you close the form with dspForm.Close() the object is not set to null. If you want your object set to null, you have to do it explicitly: dspForm = null;

user11909
  • 1,235
  • 11
  • 27
0

Use this code:

if (Application.OpenForms().OfType<dspForm>.Any()){//Your Code }
0

You shouldn´t assume any resource to be closed only because a reference to it is null. In fact having a null-reference and a disposed object don´t have anything to do which each other. You could even have two references to the same form. Would you expect both references to be null after closing the form? Why do you expect a reference to point to null after calling a method:

var m = new MyType();
m.DoSomething(); // why should this yield to m being null??

What you should be interested in instead is if your form is disposed, which will release any unmanaged ressources (such as file-handles for instance). That´s why Form implements the IDisposable-interface. Calling Close on your form will automatically call Dispose which in turn will release those resources (unless you open the form via ShowDialog). Relying on form being null however doesn´t make it release those resources and thus results in memory-leacks.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111