473,835 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1731
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.orgw rote 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******** ******@TK2MSFTN GP02.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.orgw rote 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.Control s.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
1873
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 used .Net memory profiler from Sci Tech to analyze the leaks and they are coming from inside HttpWebRequest and HttpRebResponse. When I run my code on VS2005, I do not get any leaks. Is this problem fixed in .Net 1.1 SP1? I may not be able to wait...
11
1560
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 resource, my expectation is that when t2 fails, the destructor of t1 should be called, which didn't happen. I use g++ 3.2.3 on solaris 2.8, thanks. yang
10
2350
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 several places, I have seen it said that the WinNT model only limits resources to available memory. However, I have seen errors caused by running out of resources on a machine with over 1GB of free memory (running Win2k server), as well as on my...
5
2112
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 problems I am employing the aforementioned software in trying to track down the problems. After an hour or so of program execution, a snapshot of the managed heap shows 27,025 BinaryReader objects as having been garbage collected but never...
6
8123
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 some issues when the xsl file contains scripts. The XslCompiledTransform uses the CodeDom to compile the scripts within the xsl file into .net assembly and load them into the current
5
2782
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 : myEvent -= MyMethodToFire; That's all fine, but when you use visual studio to create events for objects it never creates an unsubscribing reference, so is it puting in resource leaks? Or is this being cleared somewhere that i am not seeing?
1
4783
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 application does: 1. user provides input on a form on an aspx page and clicks continues 2. on postback, the page creates a c# .net object and calls a method on it to process the data provided by the user 3. the object in turns creates an...
1
1853
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 Initialization) or auto_ptr to wrap it. Is my understanding correct? Here is a sample about what is unmanaged resource.
3
2279
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 less. When the task is completed, there are somewhere between 4 and 7 handles still open. My process is loading images and shrinking them down. I went through all the code and found some objects that implement the IDisposable interface which I...
0
9803
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10523
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10560
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7766
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4434
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 we have to send another system
3
3088
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.