When to dispose

J

Just Me

I've been disposing whenever I create a bitmap or graphics object.

Recently I developed a bug which turned out to be as follows:

bmp=new Bitmap(...
picBox.Image = bmp
..
..
bmp.dispose
exit sub

Now I can see that Image is pointing to a disposed bitmap.
But if I don't will picturebox dispose the bitmap assigned to Image?

Do I have to assign to Image or can I just draw on it.

What happens if I never dispose of my Graphic or Bitmap objects?

Thanks in advance for any answers
 
J

Just Me

David Williams said:
1) You should dispose of the Image (in this case) not the Bitmap.
Now that's confusing. Doesn't the PictureBox need to have the Image as long
as it is displaying?
Or do I dispose the image when the Picturebox is closing?

2) Not sure what you mean by Assign to Image or Just Draw to it.

Is it better to
Dim zz As Graphics = PictureControl1.PicCreateGraphics
zz.DrawImage(Img, 0, 0)

or is it better to

picBox.Image = Img



You have to have the object (an Image) in memory before you can update it
(by drawing to on it.)

Meaning I can't do the first above before I do the second - right?
3) If you do not dispose of your Graphics or Bitmaps, most likely you will
be fine, but if you are creating huge numbers of them you may run out of
memory or other resources. The GC "should" take care of them, but I have
found that it is much bertter to dispose of them once you are done with
them. As is always the case, your application design will dictate.

There's no association between these disposes and deleting handles in
windows API (which as I remember MUST be done)?


Thanks a lot for all the info!
 

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