English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i write a code in c# that create an object but i cant dispose it

code :

private void button1_Click(object sender, EventArgs e)
{
Tnode tree = new Tnode();
tree.Dispose();

}


my class


class Tnode : IDisposable
{


public Tnode Parent;

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{

}

}


how can i dispose this object after create.
this code not work correctly.

2006-06-09 07:33:13 · 1 answers · asked by leal_boy 1 in Computers & Internet Programming & Design

1 answers

You need to write a body in the Dispose(bool) method that will release whatever unmanaged resources you're using. As it stands now, when you call dispose, you do nothing, and then you request no finalization from the system. If you're holding no unmanaged resources, I'm not sure why you'd want to do this.

You should also write a finalizer (~Tnode) that calls Dispose(false), so that your custom method is entered in any cases when the system garbage collects your object for you.

Typically your Dispose(bool) method should free any managed resources you have references to if Dispose is explicitly called (meaning the parameter is set to true). You shouldn't do this if the parameter is false, because that indicates the system is garbage collecting, and your references may not be good. Unmanaged resources are always safe* to free because the system isn't going to help you clean them.

* = unless the underlying system is not reliable, or you have bugs at unmanaged levels.

2006-06-09 08:09:36 · answer #1 · answered by Ryan 4 · 0 0

fedest.com, questions and answers