Are Graphics objects unmanaged?

D

David Veeneman

I've just read an article that says that GDI+ Graphics objects (such as the
Pen object) are unmanaged, and that the programmer must explicitly call
Dispose() on these objects in order to release membory allocated for them.
Is that correct? I haven't seen anything like that in any other GDI+
articles or documentation. Thanks in advance.
 
A

Alan Pretre

David Veeneman said:
I've just read an article that says that GDI+ Graphics objects (such as
the Pen object) are unmanaged, and that the programmer must explicitly
call Dispose() on these objects in order to release membory allocated for
them. Is that correct? I haven't seen anything like that in any other GDI+
articles or documentation.

Hi. The general rule in .NET is to always call Dispose() if the object you
are using supplies a Dispose() method. In theory the objects are black
boxes and you shouldn't need to wring your hands over whether or not you
should call it. C# makes it easy to do with the using() statement so
there's no excuse not to. Furthermore, even if you have insider knowledge
that Dispose() doesn't really do anything, that may not be true in the next
version...

-- Alan
 
F

Frank Hileman

Many objects in System.Drawing, including Graphics and Pen, are wrappers
around unmanaged GDI+ objects. Dispose is needed.

Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor
 
J

Jon Skeet [C# MVP]

Alan Pretre said:
Hi. The general rule in .NET is to always call Dispose() if the object you
are using supplies a Dispose() method. In theory the objects are black
boxes and you shouldn't need to wring your hands over whether or not you
should call it. C# makes it easy to do with the using() statement so
there's no excuse not to. Furthermore, even if you have insider knowledge
that Dispose() doesn't really do anything, that may not be true in the next
version...

That's not *quite* right - you should only Dispose of an object if you
"own" it. For instance, although Graphics implements IDisposable, you
rarely need to call Dispose on it as it is usually passed to your
method (event handler, whatever).
 
A

Alan Pretre

Jon Skeet said:
That's not *quite* right - you should only Dispose of an object if you
"own" it. For instance, although Graphics implements IDisposable, you
rarely need to call Dispose on it as it is usually passed to your
method (event handler, whatever).

Sorry, I thought that was a given.

-- Alan
 

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