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

Dispose and freeing memory

Help me out here please:

While watching Brad Abraham's MSDN TV talk about the Dispose pattern,
refering to:

public virtual void Dispose ( bool disposing )
{
if ( disposing )
{
<-- WHAT GOES HERE -->
}
}

He says: ..."here's were I'll free all my dependent objects, these are
things like managed resources... maybe you are in a form and you want to
free all the instances that are also on the form"...
I don't get that... how is Dispose going to help me free dependent objects
and managed resources? Isn't Dispose ONLY about releasing unmanaged
resources?

What am I missing?

TIA

Fernando Cacciola



Nov 17 '05 #1
6 2729
In the Dispose() method is for example called on a container control you
could call the Dipose() method of alle the contained controls

Gabriel Lozano-Morán

"Fernando Cacciola" <fe***************@hotmail.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
Help me out here please:

While watching Brad Abraham's MSDN TV talk about the Dispose pattern,
refering to:

public virtual void Dispose ( bool disposing )
{
if ( disposing )
{
<-- WHAT GOES HERE -->
}
}

He says: ..."here's were I'll free all my dependent objects, these are
things like managed resources... maybe you are in a form and you want to
free all the instances that are also on the form"...
I don't get that... how is Dispose going to help me free dependent objects
and managed resources? Isn't Dispose ONLY about releasing unmanaged
resources?

What am I missing?

TIA

Fernando Cacciola


Nov 17 '05 #2
Small correction. I believe that it should be "protected virtual void
Dispose(bool disposing)". This pattern solves the problems that occur
because your objects can be disposed in two different ways. The first is
explicitly and occurs when you call Dispose() on an object or create it via
a using statement. The second is when the garbage collector finds it.

In the first case, you are disposing the object yourself so you can have a
reasonable degree of confidence in the state of your managed objects. Thus
you can dispose them as well (if necessary).

In the case that the garbage collector disposes your objects, you do not
know there state. It is possible that the garbage collector disposed a
object to which you have a reference just before it disposed the parent
object. In that case you should only free up your unmanaged objects.

--
Jared Parsons [MSFT]
ja******@online.microsoft.com
http://blogs.msdn.com/jaredpar
"This posting is provided "AS IS" with no warranties, and confers no rights"
"Fernando Cacciola" <fe***************@hotmail.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
Help me out here please:

While watching Brad Abraham's MSDN TV talk about the Dispose pattern,
refering to:

public virtual void Dispose ( bool disposing )
{
if ( disposing )
{
<-- WHAT GOES HERE -->
}
}

He says: ..."here's were I'll free all my dependent objects, these are
things like managed resources... maybe you are in a form and you want to
free all the instances that are also on the form"...
I don't get that... how is Dispose going to help me free dependent objects
and managed resources? Isn't Dispose ONLY about releasing unmanaged
resources?

What am I missing?

TIA

Fernando Cacciola


Nov 17 '05 #3
Fernando Cacciola wrote:
Help me out here please:

While watching Brad Abraham's MSDN TV talk about the Dispose pattern,
refering to:

public virtual void Dispose ( bool disposing )
{
if ( disposing ) [Just be aware that if disposing ==true, it means that you are disposing
it yourself, so you can explicitly dispose other managed resources. It's
a good practice that you dispose the resources when you are sure that
you are not using it. It may take days for GC to come back to clean the
resources.]
{
<-- WHAT GOES HERE -->
}
}

He says: ..."here's were I'll free all my dependent objects, these are
things like managed resources... maybe you are in a form and you want to
free all the instances that are also on the form"...
I don't get that... how is Dispose going to help me free dependent objects
and managed resources? Isn't Dispose ONLY about releasing unmanaged
resources?

What am I missing?

TIA

Fernando Cacciola


Nov 17 '05 #4

"Jared Parsons [MSFT]" <ja******@online.microsoft.com> escribió en el
mensaje news:%2***************@TK2MSFTNGP15.phx.gbl...
Small correction. I believe that it should be "protected virtual void
Dispose(bool disposing)".
Yes, of course, I just typed that too fast.
This pattern solves the problems that occur because your objects can be
disposed in two different ways. The first is explicitly and occurs when
you call Dispose() on an object or create it via a using statement. The
second is when the garbage collector finds it.

In the first case, you are disposing the object yourself so you can have a
reasonable degree of confidence in the state of your managed objects.
Thus you can dispose them as well (if necessary).

In the case that the garbage collector disposes your objects, you do not
know there state. It is possible that the garbage collector disposed a
object to which you have a reference just before it disposed the parent
object. In that case you should only free up your unmanaged objects.

I understand why you can only dispose of dependent managed objects inside
the

if ( disposing )
...

branch.

My question however is about the term "free an object" and its relation with
Dispose.

To me, and I'm pretty sure that to most anyone else, to "free an object"
means to reclaim the memory it uses.
But Dispose has nothing to do with (managed) memory.

Brad says earlier in his talk that "you use Dispose to a free an object",
but that's incorrect. Unless I'm totally missing something, Dispose has
nothing to do with freeing (i.e. reclaiming memory); it has to do with
releasing (notice the different term) unmanaged resources.

I truly believe that becasue of sources like Brad's talk many people think
Dispose() is about deterministcally destroying (thus freeing) an object. It
is not.
Calling Dispose() doesn't free the object at all; it just allows the object
to release any unmanaged resources it could be using. The managed memory
block used by the object (and all its dependent objects) will still be
allocated after the call to Dispose().

Please don't get me wrong: of course nor Brad nor you erroneusly think
Dispose() reclaimns managed memory, it's obvious you both know that well. My
point is that this stuff is wrongly explained because of a bad choice of
wording: Dispose() doesn't free up any managed objects, that's the job of
the GC and you just can't do that manually. Dispose() is ONLY about
releasing resources don't managed by the GC (thus called unmanaged
resources)

Fernando Cacciola
SciSoft

Nov 17 '05 #5
Hi,

My question however is about the term "free an object" and its relation
with Dispose.


I think that the term "object" is not the best, you should say unmanaged
resource or something like that.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #6

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
escribió en el mensaje news:%2***************@TK2MSFTNGP10.phx.gbl...
Hi,

My question however is about the term "free an object" and its relation
with Dispose.


I think that the term "object" is not the best, you should say unmanaged
resource or something like that.

Right.. and I would even propose to say "release" instead of "free".

Anyway, my post was really intended to confirm that I had the right
concepts, not to nitpick on other people's errors.. even if it sounded like
that :-)

Best,

Fernando Cacciola
SciSoft
Nov 17 '05 #7

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

Similar topics

8
by: celeong | last post by:
Hi, anybody can help me with this. I've created a singleton class, and now wants to add destructor to it. I know we can implement the IDisposable and also overrides the Finalize method (from...
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...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
4
by: Larry Lard | last post by:
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...
5
by: Michael Sander | last post by:
Hi, I found out, that a simple Form, created only with the designer, wont Dispose its contextmenu if you show the form, right-click it, and dispose the form afterwards. This isnt really what I...
8
by: Jim | last post by:
In a C# project I'm working on for an iterative design application, I need to dispose of a large arrray of a struct object and reinitialize the array between iterations. That is, the user starts...
7
by: =?Utf-8?B?RnJhbnM=?= | last post by:
Hi, I'm working on an application where it is essential to free underlying non-memory resources as soon as they are no longer needed. In my case, it is a VISA resource which is used in automated...
1
by: Shaan2804 | last post by:
I have created a activex component in VB 6.0 which is using MS Word object library. I am calling this activex component in Asp.net application. This is causing the Word.exe process to run under...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.