-2

Like the default C# template in Visual Studio, I've defined a Windows Form as a static object as shown below:

public static FormMain formMain;

static void Main()
{
    formMain = new FormMain();
    Application.Run(formMain);
    formMain.Dispose();
}

As you can see, I've allocated a memory space (using new) for this static form before calling it and freed the memory (using Dispose) after the form has been closed.

However, within this static form, I've defined a couple of non-static objects (say, labels), as shown below:

public FormMain()
{
    // some code here
    Label myLabel1 = new Label();
    Label myLabel2 = new Label();
    Label myLabel3 = new Label();
    // some code here
}

Now, I've two questions:

  1. Do I have to Dispose these non-static objects as well or whether they are disposed (memory freed) as soon as the formMain.Dispose(); line is invoked?

  2. If I need to dispose these non-static objects, at what stage of the program should I prefer to use Dispose (like, in the FormClosed or in the FormClosing event)?

Note: I try not to use the form design facility in Visual Studio, but prefer to code the form line by line.

ssd
  • 2,340
  • 5
  • 19
  • 37
  • 1
    Application.Run already disposes the form in the example: "The Dispose method of the Form class will be called prior to the return of [the Application.Run] method." – user2864740 Dec 11 '16 at 20:25
  • I'm not asking about the form, but the non-static objects defined within the form (the `myLabel1`, `myLabel2`, etc). – ssd Dec 11 '16 at 20:26
  • 1
    `FormMain` should dispose member variables that it creates. It doesn't matter whether `FormMain` is being stored in a static variable. – BJ Myers Dec 11 '16 at 20:33
  • A form will dispose all of its child controls. Assuming those Labels are added to the form, their disposal is taken care of. – Mike Zboray Dec 11 '16 at 20:39
  • @mikez: In connection to your comment "**their disposal is taken care of**"; do you mean "their disposal is taken care of by the garbage collector" or "all the memory allocated within the form get freed as soon as the form is disposed"? – ssd Dec 11 '16 at 20:42
  • 1
    I mean that calling a form's Dispose method causes it to call Dispose on each one of its child controls. Also, since forms and controls have finalizers, during finalization any unmanaged resource will be cleaned up anyway. – Mike Zboray Dec 11 '16 at 20:51

1 Answers1

3

Like the default C# template in Visual Studio, I've defined a Windows Form as a static object as shown below:

Not sure why you'd want to store your application main windows form as a public static field. What particular scenario are you thinking about?

As you can see, I've allocated a memory space (using new) for this static form before calling...

This isn't C++. Calling new is mandatory in C#, it's not an option when it comes to reference types.

and freed the memory (using Dispose) after the form has been closed.

Calling Dispose frees up no managed memory at all. I suggest you read on the IDisposable pattern and the managed memory model, you seem to be misunderstanding quite a bit here.

Do I have to Dispose these non-static objects as well or whether they are disposed (memory freed) as soon as the formMain.Dispose(); line is invoked?

Static or non static is irrelevant, you should dispose all disposable objects. Read this for more information

If I need to dispose these non-static objects, at what stage of the program should I prefer to use Dispose (like, in the FormClosed or in the FormClosing event)?

You should dispose following the guidelines of the IDisposable pattern.

Community
  • 1
  • 1
InBetween
  • 32,319
  • 3
  • 50
  • 90
  • Concerning the program in question; I have **(1)** some labels that are defined within methods (like, `FormMain()`) and **(2)** some other labels that are defined within the main body of the `FormMain` class (I define them in the main body and `public` because I use them to interact with some other form). So far, I deduce that, for the labels defined with in methods (that has a scope) I do not need to dispose them. On the other hand, for the labels defined public within the main body of the form, I need to dispose them just before closing the form (in a `FormClosing` event). Am I right? – ssd Dec 11 '16 at 21:19
  • @ssd no, no and no. You should dispose objects according to the `IDisposable` pattern, forget about events. Also, the labels you are creating inside a method, if you want them to be of any use, are probably being added to form's `Controls` collection, therefore even if the local name goes out of scope, the object is still reachable. My advice: forget about forms, controls and events. You need to understand the basics first. Read up on the managed memory model, understand it and then build upon that. You seem to be trying to figure skate before even knowing how to crawl. – InBetween Dec 11 '16 at 21:28