0

I have some questions with the following (very simplified) C# code

class MyFactory{
    public static void createMyForm(){
        Config c=new Config();
        MyControl m = new MyControl();
        c.someEvent+=m.somClick;
        MyForm f=new MyForm(c);
        f.mc=m;
        f.addcontrol(f);
        f.Show();
    }
}

class Config{
    some data;
    public event someEvent;
}

// Custom control
class MyControl:Control{
}

class MyForm:Form{
    private Config config;
    public MyControl mc; // I need this here, myControl can be Menu or even Panel
    public MyForm(Config c){
        config=c;
    } 
}

Will GC handle and dispose all created objects properly? Should I implement dipose pattern to set myForm.mc to null? What about events and references?

Sanyin
  • 41
  • 2
  • 8
  • As this is to some extent specific to the UI toolkit you are using, please add the appropriate tag. – O. R. Mapper Nov 26 '14 at 09:01
  • No this code doesn't leak any memory. That said there is no short answer. Your question comprises of many questions. Refer [this](http://stackoverflow.com/questions/298261/do-event-handlers-stop-garbage-collection-from-occuring) , [this](http://stackoverflow.com/questions/2926869/do-you-need-to-dispose-of-objects-and-set-them-to-null) and [this](http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface?rq=1) – Sriram Sakthivel Nov 26 '14 at 09:06
  • Also, if you want to express that `MyControl` is a WinForms control, please do not make it seem as if it is an empty POCO. – O. R. Mapper Nov 26 '14 at 09:13

2 Answers2

2

No, it shouldn't leak as is posted. C# is capable of clearing up set of objects that reference each other, as long as there is no reference to any of these objects from a root object. Your form/config/control could leak though if for instance some other piece of code keeps a reference on Config.someEvent.

Dispose pattern is seldom required for managed memory. Dispose pattern is intended for closing external resources (file handles, sockets etc). Read Dispose Pattern:

The CLR provides support for automatic memory management. Managed memory (memory allocated using the C# operator new) does not need to be explicitly released. It is released automatically by the garbage collector (GC).

I recommend you read Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework and/or Memory Management and Garbage Collection in the .NET Framework.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
0

It depends on existance of external references to your objects.

In the sample you've provided - all your objects has only mutual references, and it's ok, no special actions required and GC will successfully collects yor objects.

But if you have somewhere in your program references from GC roots to your objects (direct or indirect) - then your objects will not be collected until that references exists.

For example, if you have somewhere list or another collection of your MyForm and adding your forms to it - this may prevent GC from collecting your forms.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71