473,508 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Disposing of Objects.

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
6 1211
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
Doh

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

Nov 21 '05 #3
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5987
by: Egghead | last post by:
Hi all, I'm just wondering that is any way to dispose all objects when my app is shut down? I mean I can use for.. next loop to unload all loaded form when I shut down my app. Thank you, ...
15
3720
by: Chris Capel | last post by:
I've heard a lot of talk on the forum about memory managment in .NET and disposing things, finalizing them, garbage collection, etc. My question is which managed types need to be disposed after...
4
1474
by: MC D | last post by:
Question: If I have a class which has a property which is a collection of widget objects (an arrayList of widgets), and both the containter class and the widget class implement the IDisposable...
5
1345
by: Mathias L. | last post by:
I have two questions for which I couldnt find answer: If I programaticaly close DialogForm (calling Close()), is it enough or do I have to dispose it as MS.NET help says? Also, in overriden...
4
1380
by: sunitabalakrishna | last post by:
Hi, Could someone explain me if I need to implement the Dispose pattern on managed objects that hold objects which have longer life or is a singleton? Example - Class A holds Class Global...
7
1135
by: Mariusz | last post by:
Hello, I have question regarding disposing objects in aspx/cs pages. If I create some objects/Arrays/ListArrays in pageLoad do I have to dispose them (by assigning the null value for objects that...
0
1509
by: Gman | last post by:
Hi, Objective: Draw a grid on a bitmap and set this as a panel's image. (Rather than draw the grid directly on the panel and redraw repeatedly in the paint event.) Problem: It works fine....
13
1922
by: Adam Right | last post by:
Hi, I am developing a financial application and you know that there are huge amount of data in these similar aplications. I have a MDIChild form and the other forms are opened in this form. For...
4
1386
by: Rob | last post by:
Hi all, I wrote myself a little function to do my own housekeeping, I can call it by using Dispose(object) Within this function there's a select case statement which will then, based on...
29
2337
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.