473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

When to dispose

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

Nov 20 '05 #1
1 2463

"David Williams" <Da***********@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
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!


HTH
--
David Williams, VB.NET MVP
"Just Me" wrote:
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

Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Cybertof | last post by:
Hi ! I'm wondering about the use of the 'using' statement. In a procedure, should i declare all instance variables in a using clause ? In the below code, what would happen if MyFont & MyFont2...
4
by: Terry | last post by:
There are a number of things about using unmanaged resources in Windows Forms programming that is unclear to me. In C++, if you loaded an icon resource using "ExtractIcon()", the resource was...
1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
2
by: Jaikumar | last post by:
Hi, 1) I have created one windows application, In the main form ( form1) i have added one usercontrol (usercontrol1), In that user control i am drawing one image. 2) In the UserControl1 i am...
9
by: Razzie | last post by:
Hey all, When should or should you not inherit from the IDisposable interface for your custom classes? Say I have a custom class SomeClass that does... some stuff. If I create a new instance...
20
by: Charles Law | last post by:
I have an application that creates a class. The class has unmanaged resources, so must end gracefully. How can I guarantee that the unmanaged resources are freed? I have looked at IDisposable,...
21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
15
by: Barry Flynn | last post by:
VB 2005. I have the following code in a Sub. Dim oFred As SillyClass oFred = New SillyClass oFred.Gloop() oFred = Nothing Exit Sub
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.