473,395 Members | 1,678 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,395 software developers and data experts.

Is this a resource leak?

Here's some code I'm using:

Image img = new Bitmap(32, 16);
PictureBox pic = new PictureBox();
pic.Image = img;
myForm.Controls.Add(pic);
// ... img now goes out of scope ...

I'm assuming that, when myForm gets closed, then pic is Disposed (as
part of the Controls collection) and at the same time Disposes of the
"new Bitmap" I assigned to its Image property. Is this correct, or do
I need to do something else to avoid a resource leak?

Eq.
Sep 11 '06 #1
2 1690
I don't see a resource leak here, looks good to me. When the form is closed,
the object 'pic' is pointing to should be eligible for GC along with the
form, as long as nothing from outside the form is pointing to it. The 'pic'
variable itself goes out of scope as soon as the method this is all in is
done running, but since the picturebox object has been added to the form's
controls collection, the form has a point to it, and the object does not get
GC'ed until the form object is no longer being used by anything.

"Paul E Collins" <fi******************@CL4.orgwrote in message
news:Hu********************@bt.com...
Here's some code I'm using:

Image img = new Bitmap(32, 16);
PictureBox pic = new PictureBox();
pic.Image = img;
myForm.Controls.Add(pic);
// ... img now goes out of scope ...

I'm assuming that, when myForm gets closed, then pic is Disposed (as part
of the Controls collection) and at the same time Disposes of the "new
Bitmap" I assigned to its Image property. Is this correct, or do I need to
do something else to avoid a resource leak?

Eq.


Sep 11 '06 #2
Whether or not this is good behavior depends on the internal dispose of the
picturebox (whether it will in turn dispose the bitmap or whether the bitmap
will hang around for finalization). Either way this is not a "leak"

Cheers,

Greg
"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:OP**************@TK2MSFTNGP02.phx.gbl...
>I don't see a resource leak here, looks good to me. When the form is
closed, the object 'pic' is pointing to should be eligible for GC along
with the form, as long as nothing from outside the form is pointing to it.
The 'pic' variable itself goes out of scope as soon as the method this is
all in is done running, but since the picturebox object has been added to
the form's controls collection, the form has a point to it, and the object
does not get GC'ed until the form object is no longer being used by
anything.

"Paul E Collins" <fi******************@CL4.orgwrote in message
news:Hu********************@bt.com...
>Here's some code I'm using:

Image img = new Bitmap(32, 16);
PictureBox pic = new PictureBox();
pic.Image = img;
myForm.Controls.Add(pic);
// ... img now goes out of scope ...

I'm assuming that, when myForm gets closed, then pic is Disposed (as part
of the Controls collection) and at the same time Disposes of the "new
Bitmap" I assigned to its Image property. Is this correct, or do I need
to do something else to avoid a resource leak?

Eq.



Sep 11 '06 #3

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

Similar topics

0
by: Steve Binney | last post by:
My code makes synchronous HttpWebRequest and HttpRebResponse calls. In VS 2003, I am getting memory leaks and event handle leaks. I am closing all streams and using "using"statements. I have...
11
by: yang su | last post by:
I have a very simple program listed below, which basically tries to use resources but prevent from memory leak should exception occurs. class T is the resource and class User is to use the...
10
by: Niall | last post by:
I'm not quite sure which groups to post this to, so short of a massive crosspost, I decided on these two. I am running into troubles with resource usage of our app on Win2k and above systems. In...
5
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
6
by: W. Jordan | last post by:
Hello there, Are there anybody who is using the XslCompiledTransform that comes with .net framework, which was said to be a replacement of the XslTransform class? I found that the class has...
5
by: Daniel | last post by:
Hey guys When you hook an event (c# 2.0 syntax): myEvent += MyMethodToFire; You need to also unsubscribe it to avoid a resource leak so that the object it is in gets garbage collected like so...
1
by: Florence Tissot | last post by:
We are seeing some kind of resource leak in our performance lab running an ASP.NET (2.0) application that sends and receives messages from 2 public MSMQ queues. Here's a brief summary of what are...
1
by: George2 | last post by:
Hello everyone, I think unmanaged resource means the resources (e.g. memory and file handler) which is used directly (new, FILE*) other than using a wrapper class (Resource Acquisition Is...
3
by: Cartoper | last post by:
My application appears to have a recourse leak. When the user starts a background process, the handle count in Process Explorer (PE) goes up by about 10, sometime 1 or 2 more, sometimes 1 or 2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.