473,626 Members | 3,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.instan ce == null)
myClass.instanc e = new myClass();

if(myClass.inst ance.disposed)
throw new ObjectDisposedE xception(myClas s.objectName);

return myClass.instanc e;
}
}

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+Finali ze = 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 2025
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.instan ce == null)
myClass.instanc e = new myClass();

if(myClass.inst ance.disposed)
throw new ObjectDisposedE xception(myClas s.objectName);

return myClass.instanc e;
}
}

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+Finali ze = 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***@vk arlsen.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******** ******@TK2MSFTN GP04.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.instan ce == null)
myClass.instanc e = new myClass();

if(myClass.inst ance.disposed)
throw new ObjectDisposedE xception(myClas s.objectName);

return myClass.instanc e;
}
}

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+Finali ze = 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*******@yaho o.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.co m>
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*******@yaho o.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
6046
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
1921
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
7658
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
6723
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
5069
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 help me to pick the right answer and explain me why ? thx. Your development team creates an order entry application by using Visual Studio .NET. The application stores and retrieves data in a
6
1799
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 resource? I understand the basic definition for managed resources are resources that the CLR manage and unmanged resources are resources that the CLR doesn't manage, however, I haven't been able to find a concrete answer as to what resources are...
156
5823
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 inside a method call. dim myvar as new object1 object1.dosomethingmethod(new object2) Note that object 2 has a dispose method but how do I dispose of it unless I do the following:
3
1946
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
1287
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 coded confuses me a bit. VS generates a Dispose subroutine, but it's not obvious if it is ok to modify it. In the New subroutine VS makes clear that you can add code to it and tell you where to add it ("Add any initialization after the...
0
8266
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
8199
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,...
0
8705
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8365
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
8505
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
7196
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...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.