Dispose GDI+ objects

F

Franz

What kind of GDI+ objects is needed to be disposed?
If they are the fields of the class , does that class need to implement the
IDispose interface?
If they are used only within a method (e.g. OnPaint), do I need to dispose
them at the end of each OnPaint method?
I want a guideline and reason for that. Thanks.
 
N

Niki Estner

Franz said:
What kind of GDI+ objects is needed to be disposed?

All that support the IDisposable interface
If they are the fields of the class , does that class need to implement the
IDispose interface?
Yes.

If they are used only within a method (e.g. OnPaint), do I need to dispose
them at the end of each OnPaint method?

If you keep them in a member to reuse them later (in another call to
OnPaint), no. Otherwise yes.
I want a guideline and reason for that. Thanks.

The guidline is quite simple: If it's got a Dispose-method, dispose it as
soon as you don't need it any more (usually when the variable goes out of
scope or you assign a new value to it)

The reason: Unmanaged handles aren't as nice as managed references are; If
you don't free them explicitly, noone does. Also, sometimes the order in
which they are released is important. So if a class directly or indirectly
contains an unmanaged handle, it usually has a Dispose method.

There are plenty of articles on this in the MSDN, if you want to know more
about it.

Niki
 

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