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

Dispose and Finalize hell!

I've read many articles including the one from Duff's blog but I've many
doubts.
public static myClass Instance
{
get
{
if (myClass.instance == null)
myClass.instance = new myClass();

if(myClass.instance.disposed)
throw new ObjectDisposedException(myClass.objectName);

return myClass.instance;
}
}

This is Singleton Instance property: it is right to throw that exception or
I must create a new object if the previous was disposed?
I know that if class A has unmanaged fields, like an IntPtr, it should
implement Dispose pattern (Dispose+Finalize = D+F)

I have:
- a class A that implements D+F
- a class B that has a field objA (of type A)
- a class C that contains Main (in a console app) or that is the main form
and has a field objB (of type B)

I think B should implement only Dispose (IDisposable) so that C can free
resources when it does not need them anymore.

I don't know why B should implement a Finalize method: if C doesn't remember
to call objB.Dispose GC will call objA.Finalize and I don't see any benefit
in implementing Finalize in B because it will be called in a non
deterministic way as for objA.Finalize!
(If I have N classes and class I use class I+1 I do not want to implement N
couples of D+F if I don't need to since Finalize has a cost.)

Moreover I think C have not to implement D or D+F: Dispose is a method to be
called by the user of a class to free unused resources, so who calls the
Dispose of the class that start the application???
Thank you all!
Luigi.
May 17 '07 #1
4 2016
BLUE wrote:
I've read many articles including the one from Duff's blog but I've many
doubts.
public static myClass Instance
{
get
{
if (myClass.instance == null)
myClass.instance = new myClass();

if(myClass.instance.disposed)
throw new ObjectDisposedException(myClass.objectName);

return myClass.instance;
}
}

This is Singleton Instance property: it is right to throw that exception or
I must create a new object if the previous was disposed?
I know that if class A has unmanaged fields, like an IntPtr, it should
implement Dispose pattern (Dispose+Finalize = D+F)

I have:
- a class A that implements D+F
- a class B that has a field objA (of type A)
- a class C that contains Main (in a console app) or that is the main form
and has a field objB (of type B)

I think B should implement only Dispose (IDisposable) so that C can free
resources when it does not need them anymore.

I don't know why B should implement a Finalize method: if C doesn't remember
to call objB.Dispose GC will call objA.Finalize and I don't see any benefit
in implementing Finalize in B because it will be called in a non
deterministic way as for objA.Finalize!
(If I have N classes and class I use class I+1 I do not want to implement N
couples of D+F if I don't need to since Finalize has a cost.)

Moreover I think C have not to implement D or D+F: Dispose is a method to be
called by the user of a class to free unused resources, so who calls the
Dispose of the class that start the application???
Thank you all!
Luigi.

You only implement finalizers on objects that contain unmanaged
resources, like that IntPtr.

In your example, I would gather only objA should have a finalizer.

The rest should have IDisposable support though so you can do a
deterministic cleanup when needed.

In the "class that start the application", Main() is static, so there
won't be an instance of this class in play, unless your program
constructs one on its own, so no, this class does not need finalizer nor
IDisposable.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
May 17 '07 #2
Something I've learned:

1. Think as there would not be Finalization system
Finalization is an anomaly of the system. An error. Should never happen.

2. Implementing IDisposable is never an error
It might do nothing useful for now, but maybe later the class grows to
manage resources that are worth of eliminating.
If all callers dispose you, you have more freedom to modify the class.
So implement IDisposable in every class and be happy.

3. Only the direct owner of the non managed resource must implement
finalizer
The rule 1 is good, but we are still humans. Just in case someone omits
Dispose. We want a safe system, after all.
In debug builds, throw Assert in finalizer to let the world know and to
catch the missing Dispose().
"BLUE" <blueha scritto nel messaggio
news:eW**************@TK2MSFTNGP04.phx.gbl...
I've read many articles including the one from Duff's blog but I've many
doubts.
public static myClass Instance
{
get
{
if (myClass.instance == null)
myClass.instance = new myClass();

if(myClass.instance.disposed)
throw new ObjectDisposedException(myClass.objectName);

return myClass.instance;
}
}

This is Singleton Instance property: it is right to throw that exception
or I must create a new object if the previous was disposed?
I know that if class A has unmanaged fields, like an IntPtr, it should
implement Dispose pattern (Dispose+Finalize = D+F)

I have:
- a class A that implements D+F
- a class B that has a field objA (of type A)
- a class C that contains Main (in a console app) or that is the main form
and has a field objB (of type B)

I think B should implement only Dispose (IDisposable) so that C can free
resources when it does not need them anymore.

I don't know why B should implement a Finalize method: if C doesn't
remember to call objB.Dispose GC will call objA.Finalize and I don't see
any benefit in implementing Finalize in B because it will be called in a
non deterministic way as for objA.Finalize!
(If I have N classes and class I use class I+1 I do not want to implement
N couples of D+F if I don't need to since Finalize has a cost.)

Moreover I think C have not to implement D or D+F: Dispose is a method to
be called by the user of a class to free unused resources, so who calls
the Dispose of the class that start the application???
Thank you all!
Luigi.
May 17 '07 #3
Laura T. <la*******@yahoo.comwrote:
2. Implementing IDisposable is never an error
It might do nothing useful for now, but maybe later the class grows to
manage resources that are worth of eliminating.
If all callers dispose you, you have more freedom to modify the class.
So implement IDisposable in every class and be happy.
I completely disagree with this. If you implement IDisposable for
*every* type you write, then everything using that type must have a
very concrete idea about the "owner" of every single instance. That
effectively gets rid of all the benefits of garbage collection, because
where you would call free() (or whatever) in a non-garbage-collected
environment, you now call Dispose.

There are some classes where it makes sense to implement IDisposable
even when you don't need it yet, but most of the time there's *very*
little chance that you'll ever need it.

Implementing IDisposable makes life harder for clients. Why make
*everyone's* life harder just on the off-chance that you might have to
make *someone's* life harder at some point in the future?

(I can only think of a *very* few situations where I've gone back and
implemented IDisposable after the fact. Certainly in less than 1% of
the types I've written.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 17 '07 #4
Laura T. <la*******@yahoo.comwrote:
>2. Implementing IDisposable is never an error
It might do nothing useful for now, but maybe later the class grows to
manage resources that are worth of eliminating.
If all callers dispose you, you have more freedom to modify the class.
So implement IDisposable in every class and be happy.
<snip>

* Jon Skeet [C# MVP] wrote, On 17-5-2007 20:06:
(I can only think of a *very* few situations where I've gone back and
implemented IDisposable after the fact. Certainly in less than 1% of
the types I've written.)
And if you need to do it, Create a new type and either inherit from the
old one, and add IDisposable. If need be, mark the old type abstract.

Or mark the old type Obsolete and copy the original contents to a
completely new type.

There currently are no ways to make sure you've called Dispose on a
Disposable object, so if someone adds it at a later time, it should be
something that comes up at compile time. The Above mentioned cases do that

Jesse
May 20 '07 #5

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();
4
by: Joe Abou Jaoude | last post by:
I m preparing to pass the 70-306 exam, so i downloaded Q & A from multiple sites. There's this question that really confuses me, coz i see that both answers A and C are both correct. Can anyone...
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...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
3
by: Boni | last post by:
Dear all, can somebody explain difference between Dispose and Finalize and when each of them should be used? Thank you very much, Boni
2
by: eBob.com | last post by:
I have a user control which creates an Excel spread sheet and badly needs Dispose/Finalize. I've read up on the subject in Balena and I think I understand what is going on. But the VS generated...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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...

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.