By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

Disposing of Objects.

P: n/a
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

--
Dennis in Houston
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
Doh

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

Nov 21 '05 #3

P: n/a
"Dennis" <De****@discussions.microsoft.com> schrieb:
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).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4

P: n/a
Thanks.

"Herfried K. Wagner [MVP]" wrote:
"Dennis" <De****@discussions.microsoft.com> schrieb:
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).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5

P: n/a
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?

"Cor Ligthert" wrote:
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

Nov 21 '05 #6

P: n/a
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

"Dennis" <De****@discussions.microsoft.com>
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?

"Cor Ligthert" wrote:
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

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.