473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Explicit freeing-up (destroying) objects

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
Nov 16 '05 #1
10 2057
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" <dm****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
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

Nov 16 '05 #2
Hi Dejan,

"dmanhr" <dm****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
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.
}
}

--
Lars Wilhelmsen
http://www.sral.org/
Software Engineer
Teleplan A/S, Norway
Nov 16 '05 #3
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
On Mon, 12 Jul 2004 14:32:01 -0700, dmanhr
<dm****@discussions.microsoft.com> wrote:
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


Nov 16 '05 #4
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

"Lars Wilhelmsen" wrote:
Hi Dejan,

"dmanhr" <dm****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
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.
}
}

--
Lars Wilhelmsen
http://www.sral.org/
Software Engineer
Teleplan A/S, Norway

Nov 16 '05 #5
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

"Lars Wilhelmsen" wrote:
Hi Dejan,

"dmanhr" <dm****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
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.
}
}

--
Lars Wilhelmsen
http://www.sral.org/
Software Engineer
Teleplan A/S, Norway

Nov 16 '05 #6
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
"Lars Wilhelmsen" wrote:
Hi Dejan,

"dmanhr" <dm****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
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.
}
}

--
Lars Wilhelmsen
http://www.sral.org/
Software Engineer
Teleplan A/S, Norway

Nov 16 '05 #7
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
"Lars Wilhelmsen" wrote:
Hi Dejan,

"dmanhr" <dm****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
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.
}
}

--
Lars Wilhelmsen
http://www.sral.org/
Software Engineer
Teleplan A/S, Norway

Nov 16 '05 #8
dmanhr <dm****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
So, there is no way to call a destructor or Finalize() method (http://msdn.microsoft.com/library/de...pc10192000.asp - i've tried that it doesn't work)?
On some pages i read a lot of reasons NOT using a GC, that is why i want to release objects explicitly (not GC).

any other solution?

thanks,

Dejan

"Jon Skeet [C# MVP]" wrote:
dmanhr <dm****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #10
dmanhr <dm****@discussions.microsoft.com> wrote:
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/de...brary/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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
12
by: f.oppedisano | last post by:
Hi, i would like to allocate two structures making only one malloc call. So i do prt=malloc(sizeof(struct1)+sizeof(struct2)); After this operation i make two pointers one to the first struct...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
4
by: Atul Sureka | last post by:
Hi, I want to free the object memory in C# - like we do using 'delete' keyword in C++. Lets say I have an object of some class and I want to explicitly free the memory. C# do not have any free...
5
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the...
1
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
9
by: david | last post by:
I will past only two segments from the code it should be enough to see what I did wrong, I think I know there I made a mistake, but how to fix it I can not tell. This why I need help from you all....
11
by: vivek | last post by:
Hello, I have a pointer to a main structure which again consists of structures, enums, char, int, float and again complex structures. When i free all the contents of the main structure, it...
25
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.