Explicit freeing-up (destroying) objects

G

Guest

Hi!

I'd like to free-up - destroy (myself not GC) object n (Node) with implementing Dispose().

I have linked list:

class Node{
CNode nextNode;
object data;

public Node(object d)
{
data = d;
}

~Node(){}

public void Dispose()
{
Console.WriteLine("Destoyed");
GC.SuppressFinalize(this);
}
}

class Test{
public static void Main(){
Node n = new Node();
n.Dispose();
}
}

If I understood text on msdn.com method GC.SuppressFinalize(this) just requests that the system not call the finalizer method for the specified object. So, what should I do/add to Dispose() method that it will explicit free (destroy) class Node? Or that code succesfuly destroys object n?

btw: I've already tried explicitly call Finalize(), but i got an error.

thanks,

Dejan
 
D

Daniel P.

You will only inform GC that you want those objects to be release. You
cannot control WHEN they will be released. At least this is the way Java
Virtual Machine does and I'm asuming CLR does the same.


dmanhr said:
Hi!

I'd like to free-up - destroy (myself not GC) object n (Node) with implementing Dispose().

I have linked list:

class Node{
CNode nextNode;
object data;

public Node(object d)
{
data = d;
}

~Node(){}

public void Dispose()
{
Console.WriteLine("Destoyed");
GC.SuppressFinalize(this);
}
}

class Test{
public static void Main(){
Node n = new Node();
n.Dispose();
}
}

If I understood text on msdn.com method GC.SuppressFinalize(this) just
requests that the system not call the finalizer method for the specified
object. So, what should I do/add to Dispose() method that it will explicit
free (destroy) class Node? Or that code succesfuly destroys object n?
 
L

Lars Wilhelmsen

Hi Dejan,

dmanhr said:
Hi!

I'd like to free-up - destroy (myself not GC) object n (Node) with
implementing Dispose().
If your Node class have references to unmanaged resources (handles, memory,
etc.) you should
implement the Dispose pattern - but if not, you really should let the GC do
it's work.

The General Dispose pattern in C#:

public class Node : IDisposable
{
public ~Node() { Dispose(false); }
public void Dispose() { Dispose(true); }
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
GC.SuppressFinalizer(this);
}
// Release unmanaged resources here.
// Like GCHandles, Open Streams etc.
}
}
 
T

Tibby

If you absolutly must destroy the object now instead of waiting for
the GC ( Security reasons, etc. ), call GC.Collect() Bewae, it's a
perfomance killer...

Tibby
 
G

Guest

Hi Lars,

Thanks for reply which was useful for me, but i have one more question about freeing-up.
Supposing that i don't have unmanaged resources whether that (your) code succesfuly destroys object "n" from memory?
As I have already said I have linked-list and when i delete some element from list (set it to null) i want to clean it up from memory (with Dispose). That's all i want ;).
So, is enough just calling GC.SuppressFinalizer(this)?

Thank you,

Dejan
 
G

Guest

Hi Lars,

Thanks for reply which was useful for me, but i have one more question about freeing-up.
Supposing that i don't have unmanaged resources whether that (your) code succesfuly destroys object "n" from memory?
As I have already said I have linked-list and when i delete some element from list (set it to null) i want to clean it up from memory (with Dispose). That's all i want ;).
So, is enough just calling GC.SuppressFinalizer(this)?

Thank you,

Dejan
 
G

Guest

Hi Lars,

Thanks for reply which was useful for me, but i have one more question about freeing-up.
Supposing that i don't have unmanaged resources whether that (your) code succesfuly destroys object "n" from memory?
As I have already said I have linked-list and when i delete some element from list (set it to null) i want to clean it up from memory (with Dispose). That's all i want ;).
So, is enough just calling GC.SuppressFinalizer(this)?

Thank you,

Dejan
 
G

Guest

Hi Lars,

Thanks for reply which was useful for me, but i have one more question about freeing-up.
Supposing that i don't have unmanaged resources whether that (your) code succesfuly destroys object "n" from memory?
As I have already said I have linked-list and when i delete some element from list (set it to null) i want to clean it up from memory (with Dispose). That's all i want ;).
So, is enough just calling GC.SuppressFinalizer(this)?

Thank you,

Dejan
 
J

Jon Skeet [C# MVP]

dmanhr said:
Thanks for reply which was useful for me, but i have one more
question about freeing-up.
Supposing that i don't have unmanaged resources whether that (your)
code succesfuly destroys object "n" from memory?
As I have already said I have linked-list and when i delete some
element from list (set it to null) i want to clean it up from memory
(with Dispose). That's all i want ;).
So, is enough just calling GC.SuppressFinalizer(this)?

No. That won't free up the memory, and indeed nothing will, short of
forcing the garbage collector to run. However, it doesn't look like you
need to implement IDisposable *or* have a finalizer - just let the GC
do its job and collect when it needs to.
 
J

Jon Skeet [C# MVP]

dmanhr said:
So, there is no way to call a destructor or Finalize() method

The Finalize() method doesn't destroy the object - it's just called
before the object *is* destroyed.
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dn
cscol/html/deepc10192000.asp - i've tried that it doesn't work)?

To be honest, that article seems to be for people wishing to write C#
as if it were C++. That's virtually never a good idea - trying to
pretend that one language is another is a disaster waiting to happen.
On some pages i read a lot of reasons NOT using a GC

Which pages, or what reasons?
that is why i want to release objects explicitly (not GC).

any other solution?

No. .NET is a managed environment. You can't bypass garbage collection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top