Disposing of Objects.

G

Guest

I have a global variable that is of type Image. I set this to different
images in different routins using BitBlt to copy from the screen. If I
dispose of the variable image after I'm thru with each image, not only the
image is disposed but also the variable and I can't reference it again.
Question is do I need to dispose of the old image each time I change the
variable's image or will the old image be disposed by the garbage collector?
I have the same problem with a font variable. Thanks for any answer.

dim myimage as bitmap

private sub dosomething()
myimage = get_imageno1
myimage.dispose
......
end sub

Private sub dosomethingelse()
myimage = get_imageno2
.........
end sub
 
C

Cor Ligthert

Dennis,

Why you make your Image global when you want to dispose it all the time, is
this not more efficient in the way you are using it.

\\\
dim myimage as bitmap
private sub dosomething()
dim myimage as bitmap
myimage = get_imageno1
myimage.dispose
......
end sub
///

Cor
 
C

Cor Ligthert

Doh

\\\
private sub dosomething()
dim myimage as bitmap
myimage = get_imageno1
myimage.dispose
.......
end sub
///
 
H

Herfried K. Wagner [MVP]

Dennis said:
I have a global variable that is of type Image. I set this to different
images in different routins using BitBlt to copy from the screen. If I
dispose of the variable image after I'm thru with each image, not only the
image is disposed but also the variable and I can't reference it again.

You can use the variable, but you cannot/should not access its properties if
the object the variable is pointing to is disposed. In order to be able to
access instance members, you'll need to instantiate a new object and assign
it to the variable:

\\\
myimage = New Bitmap(...)
....
///
Question is do I need to dispose of the old image each time I change the
variable's image or will the old image be disposed by the garbage
collector?

It will be disposed if the GC calls the object's finalizer before actually
destroying the object. Nevertheless it's better to call 'Dispose'
explicitly in order to keep memory usage and usage of unmanaged resources
(GDI handles, for example), small. It's the same for 'Font' objects, pens,
brushes, and 'Graphics' objects (and some other objects).
 
G

Guest

Thanks.

Herfried K. Wagner said:
You can use the variable, but you cannot/should not access its properties if
the object the variable is pointing to is disposed. In order to be able to
access instance members, you'll need to instantiate a new object and assign
it to the variable:

\\\
myimage = New Bitmap(...)
....
///


It will be disposed if the GC calls the object's finalizer before actually
destroying the object. Nevertheless it's better to call 'Dispose'
explicitly in order to keep memory usage and usage of unmanaged resources
(GDI handles, for example), small. It's the same for 'Font' objects, pens,
brushes, and 'Graphics' objects (and some other objects).
 
G

Guest

I need it to be global because I use it in different mouse events. For
example, in the mousedown event, I copy a rectangular area of my panel (only
part of the panel) using bitblt to a global bitmap object. In the mousemove
event, I draw something else in the area of the rectangle. In the MouseUP
event, I replace the original rectangular area with the original bitmap to
restore the area to what it was before I drew on it in the mouse move event.
How do I do this without using a global variable assigned to the original
rectangle?
 
C

Cor Ligthert

Dennis,

When you need to use it global than you can not dispose it in my opinion.

What you want to use when it is not anymore there?

Cor
 

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