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

B

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??
 
J

Jon Skeet [C# MVP]

Bob said:
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.
 
B

Bob

Thanks a lot Jon, Now it all makes sense to me.


Jon Skeet said:
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.
 
B

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
 
J

Jon Skeet [C# MVP]

Bob said:
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.
 
B

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.
 
J

Jon Skeet [C# MVP]

Bob said:
"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).
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top