473,385 Members | 1,356 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,385 software developers and data experts.

Question on Dispose / IDispose

Ran across some code on the net that incorporates "dispose" 2
different ways..

I understand a calling routine could use the .Dispose on your class,
but I was under the impression "Dispose()" was the same thing as the
deconstructor in the following example (~YourClassNameHere()).

What would cause the deconstructor to fire off over the "Dispose()"
routine?

Would the deconstructor get called first, and then the Dispose() last?
If so, why not just have one simple "Dispose()" routine?


Snapshot of the Code:

internal class YourClassNameHere : MarshalByRefObject, IDisposable

~YourClassNameHere()
{
dispose( false );
}

public void Dispose()
{
dispose( true );
}

private void dispose( bool disposing )
{
if( disposing )
{
a = null;
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect( 0 );
}
}
Nov 16 '05 #1
1 3901
The "deconstructor" or as its known in .NET, the Finalizer, has a similar function to Dispose but different semantics and usage pattern. Rather than write a huge tract on this I'll refer you to Chris Lyon's excellent description of their relationship

http://blogs.msdn.com/clyon/archive/...21/232445.aspx

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<55**************************@posting.google.com >

Ran across some code on the net that incorporates "dispose" 2
different ways..

I understand a calling routine could use the .Dispose on your class,
but I was under the impression "Dispose()" was the same thing as the
deconstructor in the following example (~YourClassNameHere()).

What would cause the deconstructor to fire off over the "Dispose()"
routine?

Would the deconstructor get called first, and then the Dispose() last?
If so, why not just have one simple "Dispose()" routine?


Snapshot of the Code:

internal class YourClassNameHere : MarshalByRefObject, IDisposable

~YourClassNameHere()
{
dispose( false );
}

public void Dispose()
{
dispose( true );
}

private void dispose( bool disposing )
{
if( disposing )
{
a = null;
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect( 0 );
}
}

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.770 / Virus Database: 517 - Release Date: 27/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #2

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

Similar topics

2
by: Kevin Phifer | last post by:
If you are not creating a finializer in your class, but you are implementing IDisposable, is it still a good idea to call GC.SupressFinialize inside the dispose method? Does this somehow make...
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...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
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...
9
by: Alvin Bruney [MVP] | last post by:
with the using construct, exit from scope calls dispose on said object. But what happens in a connection pooling scenario? Is the run-time smart enough to realize that Object Pooling is being used...
13
by: | last post by:
I'm curious if anyone knows why the C# and VB.NET compilers don't automatically call Dispose() on objects that support IDisposable when they go out of scope. I asked a co-worker and his response...
0
by: Joe | last post by:
Reposting here as there were no useful replies in the dotnet.framework NG... What is the correct pattern for handling exceptions in IDisposable.Dispose, especially in a class that manages...
6
by: Teresa | last post by:
1) If I do want to keep an object alive throughout the live of an application, how can I ensure that the GC doesn't clean it up? 2a) How do I determine if an object is a managed or an unmanged...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

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.