Idisposable

G

Guest

VB.Net Documentation for implementing IDisposable has:

Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
If disposing Then
' Free other state (managed objects).
End If
Tag.Dispose()
' Free your own state (unmanaged objects).
' Set large fields to null.
End Sub

I am having trouble understanding the comments:
What does "state (managed objects)" mean
What does "state (unmanaged objects)" mean
I understand managed objects and unmanaged objects like com objects, certain
GDI objects are unmanaged and should be disposed here. But what does the
"state" mean?
 
S

Scott M.

It just means that the object would free itself from whatever resources it
was holding, thus it would no longer be statefull. Don't let the wording
hang you up.
 
G

Guest

Thanks. I guess the State just means that the instance of it exists and the
managed objects will free themselves (sometime) whereas I have to free all my
own state unmanaged objects like the GDI pens, etc.
 
G

Guest

Scott, it would be very helpful if you could give me an example of :

a State Managed Object and a "your own State unmanaged object. For example,
is a GDI Pen "state managed object" or my own state managed object"

I really appreciate you helping me to understand this.
 
S

Scott M.

Hi Dennis,

I really don't know what a GDI Pen object is (I have no experience with it.
Having said that, it's really pretty simple...managed objects are classes
that are instanced from and their code is run 100% by the Common Language
Runtime. Many of the classes in the .NET Framework are 100% managed
objects. Some however, are not. An OLEDBConnection is not 100% managed
because the underlying OLEDB Provider is COM based.

An unmanaged object is a class that is NOT instanced by the CLR. So, any
non .NET objects (COM for example) would qualify here. Examples of
unmanaged objects would be any ActiveX control or other COM objects like
automating Office classes.
 
H

Herfried K. Wagner [MVP]

Dennis,

Dennis said:
a State Managed Object and a "your own State unmanaged object. For
example,
is a GDI Pen "state managed object" or my own state managed object"

You should call a pen's 'Dispose' method explicitly when the class'
'Dispose' method is called.
 
G

Guest

Thanks...I understand a lot more now. I guess I can tell if a .net object is
100% managed if it doesn't have a dispose method and vice versa, if it has a
dispose method then it's not 100% managed.
 
C

Cor Ligthert

Dennis,

IDisposable defines a method to release allocated unmanaged resources.
Unfortunality is this very inconsequently used (even on MSDN) and is as well
spoken about unmanaged objects, what gives in my idea the big
misunderstandings about this.

In my opinion does dotNet itself not know unmanaged objects.

Just my thought,

Cor
 
G

Guest

I agree. You'd think that M'soft could write a system like .Net that knew
what an unmanaged object is. Better yet, why don't they finish .Net so
unmanaged objects aren't needed. I get tired of "disposing" of simple things
like Pens, Brushes, Data Base connections, etc. Every time I use one of the
..Net objects, I have to check to see if it has a Dispose method.
 
C

Chad Z. Hower aka Kudzu

=?Utf-8?B?RGVubmlz?= said:
I agree. You'd think that M'soft could write a system like .Net that
knew what an unmanaged object is. Better yet, why don't they finish
.Net so unmanaged objects aren't needed. I get tired of "disposing" of
simple things like Pens, Brushes, Data Base connections, etc. Every
time I use one of the Net objects, I have to check to see if it has a
Dispose method.

I agree that is a major PITA. For one - using should be able to be used
WHETHER or not it has a dispose...

But the underyling model of unmanaged vs managed is solid - and we will
always need the option for finalizatino and unmanaged objects. The problem
comes from a less than optimal implementation in the C# language (and even
worse in VB). Im not going to tout that Delphi is better - but how Borland
did Delphi.NET IMO is a lot more consistent and cleaner in many ways. In some
ways its less clean though because of compatibility with non .NET Delphi
though.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
J

Jay B. Harlow [MVP - Outlook]

Dennis,
Better yet, why don't they finish .Net so
unmanaged objects aren't needed.
IDisposable will still be needed/usable!

Consider limited 100% managed resources that are handed out my some form of
manager/repository class. IDisposable can be used to inform the object that
you are done with it, allowing the object to inform the manager/repostitory
its free now. Both the manager/repostitory & the class itself are 100%
managed...


VB.NET 2005 (aka Whidbey, due out later in 2005) in fact simplifies
IDisposable by adding the Using statement.

http://msdn2.microsoft.com/library/htd05whh.aspx

Using thePen As New Pen(...)
gr.DrawLine(thePen, ...)
gr.DrawLine(thePen, ...)
gr.DrawLine(thePen, ...)
gr.DrawLine(thePen, ...)
End Using

Then you only need to worry about using the pen, and not worry about
IDisposable or a Try/Finally.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top