473,698 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dispose() and GC.SuppressFina lize()???

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.SuppressFina lize().
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.Diagnost ics.Debug.Write Line("This is disposed");
GC.SuppressFina lize(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 6344
Bob <bo*******@yaho o.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.SuppressFina lize().
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.Diagnost ics.Debug.Write Line("This is disposed");
GC.SuppressFina lize(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 SuppressFinaliz e 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 SuppressFinaliz e because you haven't defined a finalizer anyway.

--
Jon Skeet - <sk***@pobox.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Bob <bo*******@yaho o.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.SuppressFina lize(). 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.Diagnost ics.Debug.Write Line("This is disposed");
GC.SuppressFina lize(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 SuppressFinaliz e 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 SuppressFinaliz e because you haven't defined a finalizer anyway.

--
Jon Skeet - <sk***@pobox.co m>
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.P age 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Bob <bo*******@yaho o.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.SuppressFina lize(). 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.Diagnost ics.Debug.Write Line("This is disposed");
GC.SuppressFina lize(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 SuppressFinaliz e 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 SuppressFinaliz e because you haven't defined a finalizer anyway.

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

Nov 18 '05 #4
Bob <bo*******@yaho o.com> wrote:
I have a different question on Dispose. The Dispose() method of the
System.Web.UI.P age 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Bob <bo*******@yaho o.com> wrote:
I have a different question on Dispose. The Dispose() method of the
System.Web.UI.P age 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 18 '05 #6
Bob <bo*******@yaho o.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.co m>
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
6059
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 Dispose method of IDbConnection. This Dispose method is called from a destructor. In the dispose method, i also call GC.SuppressFinalize(this) so as to avoid finalizer. All this was OK untill i came across one article that says - not to call...
4
1924
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 release refrence from memory ? E. If it release object from memory then what GC does ? 2. Which class can have dispose method ? class which is member of IDispose Inteface 3. Where we should use abstract / Interface ?
24
7673
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 Dispose-Method and call the GC nothing happend!!! my Disposemethod has never been called!! so the GC dont call my Dispose-Method although I implemented IDisposable? what am i doing wrong?
4
6727
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
9964
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 SaveXLReport method completes successfully, calls myXLClass.Dispose, and returns control to the user form at a base state. At this point, there is still an EXCEL.EXE in processes and the specified workbook cannot be saved until that process is ended from...
3
2072
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 happen if I just create my own Dispose (or any other name) mehtod, without implementing the IDisposable interface. In all the examples, which I found, this Dispose method called from my user code (not by GC). What do I miss? Thanks.
1
1724
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 SQLConnection. My db access class has a method that returns a dataset and one that executes a non-reader request. It also has a dispose method (to release the connection) that I call in the finalize method of my aspx pages(as learn in "Implementing...
44
8229
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 End Class now where would I properly dispose of this? In the finalize method? I am loading the data for the table in a subroutine that is executed at form load, of course all the commands tied to it are wrapped in using blocks, but
0
968
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 GC.SuppressFinalize(this); in the same method?
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7721
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.