Using 'Dispose' in a Custom Paint Event

G

Guest

I have an About box where I've successfully implemented a custom paint event.
The structure of it is simple:

protected void PaintClient(Object sender, PaintEventArgs e)
{
// Assorted graphics code goes here
}


Following examples I've seen elsewhere I added the following as the last line:
e.Dispose();

But this caused the program to crash. Why is that? Is it not necessary to
dispose of this object after using it to draw assorted graphics?
 
T

Tim Wilson

No. You don't dispose of anything that "doesn't belong to you". In other
words, since you didn't "allocate it" then you shouldn't "destroy it". You
should dispose of pens, brushes, etc. that you instantiated and used during
the paint routine but not objects passed to the paint handler.
 
R

Richard Blewett [DevelopMentor]

Robert W. said:
I have an About box where I've successfully implemented a custom paint
event.
The structure of it is simple:

protected void PaintClient(Object sender, PaintEventArgs e)
{
// Assorted graphics code goes here
}


Following examples I've seen elsewhere I added the following as the last
line:
e.Dispose();

But this caused the program to crash. Why is that? Is it not necessary
to
dispose of this object after using it to draw assorted graphics?

Because you don;t own that pbject, the WinFor,ms framework has given you an
object to use they will clean it up.

If you allocate a Disposble resource you should ccall Dispose. If you get
handed one, unless there is a defined protocol for who owns it, leave it
well alone

Regards

Richard Blewett - DevelopMentor
http://www.dotnteconsult.co.uk/weblog
http://www.dotnteconsult.co.uk
 

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