3 Replies - 289 Views - Last Post: 21 June 2012 - 07:25 AM Rate Topic: -----

#1 Zigon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Implementing Dispose and/ or Finalize to a class

Posted 21 June 2012 - 02:08 AM

I've been reading about disposing objects when they are not needed any more - some questions were raised:
1: Should one implement/ inherit IDisposable to custom class, so it can be disposed when needed?
2: Shoud one even wory about No.:1?
3: Will GC take care of cleaning all the resources even when my object is not being destroyed?

For example:

If I have a class (pseudo)
class MyClass
	{
	//do some stuff
	}



and use it (pseudo)
MyClass Foo = New MyClass();
Foo.DoSomeStuff

Foo.Dispose(); <- ain't there (not implemented...)
Foo.Close(); <- ain't there (not implemented also...)
Foo=null; <- is it worth?



So, what would be the best way to deal with it? Or should I just let the GC take care of everything?

Is This A Good Question/Topic? 0
  • +

Replies To: Implementing Dispose and/ or Finalize to a class

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Implementing Dispose and/ or Finalize to a class

Posted 21 June 2012 - 03:41 AM

If your MyClass contains references to objects which forbid GC to collect it, then you should implement IDisposable and call Dispose when you want to get rid of MyClass.

For example this code causes memory problems because it doesn't get rid of MyClass when Button is clicked and MyClass keeps a reference to X.
class X : Form
{
  Button_Click() {
    var foo = new MyClass(X);
  }
}

class MyClass
{
  private Form x;

  public MyClass(Form x)
  {
    this.x = x;
    this.x.SomeWeirdEvent += EventOccurred;
  }

  public void EventOccurred() 
  {  }
}



To fix it, we should change our code to this:
class X : Form
{
  Button_Click() {
    var foo = new MyClass(X);
    // Dispose when not needed anymore.
    foo.Dispose(); 
  }
}

class MyClass : IDisposable
{
  private Form x;

  public MyClass(Form x)
  {
    this.x = x;
    this.x.SomeWeirdEvent += EventOccurred;
  }

  public void EventOccurred() 
  {  }

  public void Dispose()
  {
    // Remove event handlers when class is disposed.
    this.x.SomeWeirdEvent -= EventOccurred;
    this.x = null; // not required, GC should be able to handle this class even without setting x to null.
  }
}



edit:
Also when Dispose is provided, you should use it (Streams etc. might lock files if you don't dispose them).

And about question #3: GC will collect items when no other class is referencing them anymore. (event handlers keep references alive so remember to get rid of them).


Correct me if I'm wrong, I haven't really studied GC that much but I'm left with these impressions.

This post has been edited by janne_panne: 21 June 2012 - 03:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,718
  • Joined: 08-June 10

Re: Implementing Dispose and/ or Finalize to a class

Posted 21 June 2012 - 07:02 AM

This MSDN page explains how to implement IDisposable and a Finalizer:

http://msdn.microsof...disposable.aspx

Basically, Finalizers are for cleaning up non-managed resources, if you have any. The rest, you want to clean up in Dispose.
Was This Post Helpful? 0
  • +
  • -

#4 Zigon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Re: Implementing Dispose and/ or Finalize to a class

Posted 21 June 2012 - 07:25 AM

I see... Ok! Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1