473,396 Members | 2,099 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,396 software developers and data experts.

Dispose() and GC.SuppressFinalize()???

Bob
When implementing the Dispose() method, I understand that it's supposed to
do what Finalize() does so that's why it should call GC.SuppressFinalize().
However, it's not clear to me what happens the instance itself, for example
in the following code:

public class Test1 : IDisposable {
public string Hello() {
return "HEllo";
}

public void Dispose() {
System.Diagnostics.Debug.WriteLine("This is disposed");
GC.SuppressFinalize(this);
}
}

I don't see how an instance of this class can be destroyed as there is not
something like "this = null;" in Dispose(), and also Finalize() won't be
called. Does the runtime somehow know that the instance of this class has a
Dispose() method so it should go ahead destroy the instance once the
Dispose() method is called??
Nov 18 '05 #1
6 6328
Bob <bo*******@yahoo.com> wrote:
When implementing the Dispose() method, I understand that it's supposed to
do what Finalize() does so that's why it should call GC.SuppressFinalize().
However, it's not clear to me what happens the instance itself, for example
in the following code:

public class Test1 : IDisposable {
public string Hello() {
return "HEllo";
}

public void Dispose() {
System.Diagnostics.Debug.WriteLine("This is disposed");
GC.SuppressFinalize(this);
}
}

I don't see how an instance of this class can be destroyed as there is not
something like "this = null;" in Dispose(), and also Finalize() won't be
called. Does the runtime somehow know that the instance of this class has a
Dispose() method so it should go ahead destroy the instance once the
Dispose() method is called??


No. Dispose and garbage collection are entirely separate - but the
finalizer isn't required for something to be garbage collected. A
finalizer only lists *extra* things which need to be done before
garbage collection, and the call to SuppressFinalize basically tells
the GC that there's no need to call the finalizer because those things
have already been done. Note that in your code above, there's no nee to
call SuppressFinalize because you haven't defined a finalizer anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 18 '05 #2
Bob
Thanks a lot Jon, Now it all makes sense to me.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bob <bo*******@yahoo.com> wrote:
When implementing the Dispose() method, I understand that it's supposed to do what Finalize() does so that's why it should call GC.SuppressFinalize(). However, it's not clear to me what happens the instance itself, for example in the following code:

public class Test1 : IDisposable {
public string Hello() {
return "HEllo";
}

public void Dispose() {
System.Diagnostics.Debug.WriteLine("This is disposed");
GC.SuppressFinalize(this);
}
}

I don't see how an instance of this class can be destroyed as there is not something like "this = null;" in Dispose(), and also Finalize() won't be
called. Does the runtime somehow know that the instance of this class has a Dispose() method so it should go ahead destroy the instance once the
Dispose() method is called??


No. Dispose and garbage collection are entirely separate - but the
finalizer isn't required for something to be garbage collected. A
finalizer only lists *extra* things which need to be done before
garbage collection, and the call to SuppressFinalize basically tells
the GC that there's no need to call the finalizer because those things
have already been done. Note that in your code above, there's no nee to
call SuppressFinalize because you haven't defined a finalizer anyway.

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

Nov 18 '05 #3
Bob
I have a different question on Dispose. The Dispose() method of the
System.Web.UI.Page class is called automatically when the page object goes
out of scope. Is this called by the ASP.NET process or the .NET runtime?
This is particularly interesting to me as it would be great that I can
implement my own class to be like this, that is, the Dispose() method would
be executed automatically, without the calling class having to do "Using
{...}" or call it explicitly.

Thanks
Bob

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bob <bo*******@yahoo.com> wrote:
When implementing the Dispose() method, I understand that it's supposed to do what Finalize() does so that's why it should call GC.SuppressFinalize(). However, it's not clear to me what happens the instance itself, for example in the following code:

public class Test1 : IDisposable {
public string Hello() {
return "HEllo";
}

public void Dispose() {
System.Diagnostics.Debug.WriteLine("This is disposed");
GC.SuppressFinalize(this);
}
}

I don't see how an instance of this class can be destroyed as there is not something like "this = null;" in Dispose(), and also Finalize() won't be
called. Does the runtime somehow know that the instance of this class has a Dispose() method so it should go ahead destroy the instance once the
Dispose() method is called??


No. Dispose and garbage collection are entirely separate - but the
finalizer isn't required for something to be garbage collected. A
finalizer only lists *extra* things which need to be done before
garbage collection, and the call to SuppressFinalize basically tells
the GC that there's no need to call the finalizer because those things
have already been done. Note that in your code above, there's no nee to
call SuppressFinalize because you haven't defined a finalizer anyway.

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

Nov 18 '05 #4
Bob <bo*******@yahoo.com> wrote:
I have a different question on Dispose. The Dispose() method of the
System.Web.UI.Page class is called automatically when the page object goes
out of scope.
I doubt that very much. It's certainly not just because it goes out of
scope.
Is this called by the ASP.NET process or the .NET runtime?
This is particularly interesting to me as it would be great that I can
implement my own class to be like this, that is, the Dispose() method would
be executed automatically, without the calling class having to do "Using
{...}" or call it explicitly.


Are you sure that whatever's *creating* the Page isn't also disposing
it? Or possibly you're seeing finalization?

To be honest, I don't have much ASP.NET experience, but the CLR
certainly isn't disposing anything based just on scope.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 18 '05 #5
Bob
"goes out of scope" is probably not the right way to describe it. What
happens is once the page processing completes, the Dispose() method would be
called. I'm not sure exactly where it happens but it happens because any
code put in the method would be executed. If the .NET runtime doesn't do it
then it's ASP.NET process doing it. I'm interested in knowing how it's
implemented.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bob <bo*******@yahoo.com> wrote:
I have a different question on Dispose. The Dispose() method of the
System.Web.UI.Page class is called automatically when the page object goes out of scope.


I doubt that very much. It's certainly not just because it goes out of
scope.
Is this called by the ASP.NET process or the .NET runtime?
This is particularly interesting to me as it would be great that I can
implement my own class to be like this, that is, the Dispose() method would be executed automatically, without the calling class having to do "Using
{...}" or call it explicitly.


Are you sure that whatever's *creating* the Page isn't also disposing
it? Or possibly you're seeing finalization?

To be honest, I don't have much ASP.NET experience, but the CLR
certainly isn't disposing anything based just on scope.

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

Nov 18 '05 #6
Bob <bo*******@yahoo.com> wrote:
"goes out of scope" is probably not the right way to describe it. What
happens is once the page processing completes, the Dispose() method would be
called. I'm not sure exactly where it happens but it happens because any
code put in the method would be executed. If the .NET runtime doesn't do it
then it's ASP.NET process doing it. I'm interested in knowing how it's
implemented.


Well, not knowing much about ASP.NET, I would assume that there's
basically some kind of try/finally (effectively) so that whatever
creates the page also then destroys it, just as in the normal kind of
situation of:

using (Something = new Something())
{
....
}

invokes the Dispose method of Something (which must implement
IDisposable).

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

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

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
4
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
4
by: Sunit Joshi | last post by:
Hello All I have an abstract class C1 with this: public abstract class C1 { protected bool m_Dirty; protected override void Dispose(bool disposing) { if(m_Dirty) WriteOuput();
3
by: Vagabond Software | last post by:
Scenario: The end-user selects File-Save from my applications (WinForm) menu, specifies a filename, and waits waits for my app to write the contents of a dataset to an Excel workbook. The...
3
by: Maxim | last post by:
Hi! According to documenation, if we need to release some umanaged resources manually, we need to implement IDisposable interface with single Dispose method. I am just wondering, what will...
1
by: Billy | last post by:
Hello... I'm trying to make a database access class for an asp.net application. When I run my application, the Garbage Collecter doesn't seems to unload the memory attributed to my...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: cksrajapplabs | last post by:
I am using User Controls in my Win Forms. How to dispose those controls by using Dispose() method? I am calling base.Dispose(); method in the Dispose() method. Is it must to call...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.