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

Home Posts Topics Members FAQ

Dispose / Finalize - not 'getting' it

I still don't really 'get' this. Why can't an object just release its
resources in its Finalize method? Why when I create (eg) a Graphics
object does it become *my* responsibility to say when I've finished
with it, and explcitly say so? I thought one of the points of managed
memory / garbage collection was that users of objects didn't have to
worry about tracking object lifetime?

What's the point of me saying g.Dispose at the end of my drawing
routine, when this is going to happen when g dies anyway?

Why do I have to check with every object I use whether or not it
implements IDisposable, and if it does then track its lifetime myself?
How is this better or more reliable than manual reference counting?

Lots of question marks but only one question, really. Any insight
welcome!

--
Larry Lard
Replies to group please

Dec 6 '05 #1
4 1255
dgk
On 6 Dec 2005 09:21:22 -0800, "Larry Lard" <la*******@hotm ail.com>
wrote:
I still don't really 'get' this. Why can't an object just release its
resources in its Finalize method? Why when I create (eg) a Graphics
object does it become *my* responsibility to say when I've finished
with it, and explcitly say so? I thought one of the points of managed
memory / garbage collection was that users of objects didn't have to
worry about tracking object lifetime?

What's the point of me saying g.Dispose at the end of my drawing
routine, when this is going to happen when g dies anyway?

Why do I have to check with every object I use whether or not it
implements IDisposable, and if it does then track its lifetime myself?
How is this better or more reliable than manual reference counting?

Lots of question marks but only one question, really. Any insight
welcome!


Seems to be an issue of managed resources vs unmanaged resources.
Helps not, right?

Well, anyway, in VB2005 you can use USING (no, not the one at the top
of the Class) to automatically dispose of those pesky unmanaged
resources. Like this:

Dim XXX As System.Drawing. Graphics
Using XXX
' do something here with XXX
End Using

and POOF! XXX is gone. As is, I think, anything else declared in the
construct.

So, all you have to do now is know what objects require Using - which
sort of leaves us where we started.

This is from the help file:
Sometimes your code requires an unmanaged resource, such as a file
handle, a COM wrapper, or a SQL connection. A Using block guarantees
the disposal of one or more such resources when your code is finished
with them. This makes them available for other code to use.
Dec 6 '05 #2
"Larry Lard" <la*******@hotm ail.com> schrieb
I still don't really 'get' this. Why can't an object just release
its resources in its Finalize method? Why when I create (eg) a
Graphics object does it become *my* responsibility to say when I've
finished with it, and explcitly say so? I thought one of the points
of managed memory / garbage collection was that users of objects
didn't have to worry about tracking object lifetime?

What's the point of me saying g.Dispose at the end of my drawing
routine, when this is going to happen when g dies anyway?

Why do I have to check with every object I use whether or not it
implements IDisposable, and if it does then track its lifetime
myself? How is this better or more reliable than manual reference
counting?
It's better because you don't have to clean it up on your own. Automatic
memory management is related to the .Net objects. You don't have to call
'GlobalAlloc' or 'GlobalFree' anywhere to reserve/release memory.

Concerning Dispose: You are usually not forced to call it, but Dipose gives
you the ability to free resources *immediatelly* without having to wait for
the GC to collect the objects and freeing resources in their finalize
methods. This is helpful because some unmanaged resources are limited. If
you would wait for the object to be destroyed, you might run into a lack of
free resources, e.g. database connections or limited GDI objects on older
OS versions or other limited (system) resources.

So, the distinction is: managed resources are almost unlimited, i.e. the
limit is usually the RAM available, and the GC handles this, whereas
unmanaged resources might be limited or, in some cases, allocation and
destruction of resources even must be used in pairs.
Lots of question marks but only one question, really. Any insight
welcome!


Armin

Dec 6 '05 #3
Why can't an object just release its
resources in its Finalize method?


In addition to what the others said, there are also sometimes thread
affinity reasons. Some native resources must be released on the same
thread as they were allocated. Since finalizers run on a dedicated
thread it may not be possible to free the resource from Finalize.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 6 '05 #4
Mattias,

It's is interesting that you mention that because I just started a
thread about that very topic a few days ago in
microsoft.publi c.dotnet.genera l. I suspect that it's unlikely that
anyone would have to deal with that issue. I don't know of many APIs
that have thread affinity issues.

Brian

Mattias Sjögren wrote:
Why can't an object just release its
resources in its Finalize method?


In addition to what the others said, there are also sometimes thread
affinity reasons. Some native resources must be released on the same
thread as they were allocated. Since finalizers run on a dedicated
thread it may not be possible to free the resource from Finalize.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Dec 6 '05 #5

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

Similar topics

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...
71
10720
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or is that automatic? Thanks
4
2025
by: BLUE | last post by:
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();
0
8265
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
8196
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
8637
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...
0
8504
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
5574
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
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2625
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
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.