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

Memory Leakage Due to ICON

I have an ImageList (_imageList). In _imageList I have put large numbers of Icons.
Now what I need is to get Handle of all Icons that I put in _imageList, so that I can destroy (DestoryIcon) them all and release the memory because it's causing Memory leakage.
Nov 19 '08 #1
11 2253
I have thousands of Icon in my child application (A). All these icons are in ImageList (_imageList). At the end of application 'A', I have Destroyed _imageList. But, the momory being occupied by hundreds of Icons are still there causing memory leakage. So, whats the solution.

Expand|Select|Wrap|Line Numbers
  1. private void ChildForm_Load(object sender, EventArgs e)
  2.         {
  3.             Random r = new Random();
  4.             for (int i = 0; i < 4000; i++)
  5.             {
  6.                 int index = r.Next(1, 6);
  7.                 using (SysImageList sil = new SysImageList()) //SysImageList is class containing function to get Icon.
  8.                 {
  9.                     System.Drawing.Icon ico = sil.Icon(index); //SysImagelist.Icon(int i) is the function to get the Icon using index.
  10.                     if (ico != null)
  11.                     {
  12.                         imageList1.Images.Add(ico);
  13.                     }
  14.                 }
  15.             }
  16.         }
  17.  
  18. [DllImport("comctl32.dll", CharSet = CharSet.Auto)]
  19.  public static extern bool ImageList_Destroy(IntPtr hImageList);
  20. -------------
  21. protected override void Dispose(bool disposing)
  22.         {
  23.  
  24.         bool destroyImgList = ImageList_Destroy(imageList1.Handle); //Returning always true.
  25.  
  26.             if (disposing && (components != null))
  27.             {
  28.                 components.Dispose();
  29.             }
  30.             base.Dispose(disposing);
  31.         }


But still there is memory leakage. Due to System.Drawing.Icon ico = sil.Icon(index);

So, how to handle this memory leakage

edit by mod: Please use [code] tags, not[indent] tags.
Nov 19 '08 #2
r035198x
13,262 8TB
ImageList.Images gives you all the images ...
Nov 19 '08 #3
I know but thats the Image. Image is not convertable to Icon.....If we use
((Bitmap)image).Hicon()) to get Handle to Icon and destroy it. It's no meaning because it create and destory that Icon.
Nov 19 '08 #4
Curtis Rutland
3,256 Expert 2GB
Please don't double post your questions. If you made a mistake and need to change your question, you can click the Edit button to edit your post, or you can post your corrections in a reply to your original thread. If you can't find your original thread, click the "My Subscriptions" link near the top of the page. If you feel that your question has been overlooked, post a reply to it to "bump" it back to the top of the forum. We ask that you do this only once every 24 hours.

So there is no reason to double post. It makes it hard on the Experts and you to keep track of what help you've already been given.

Threads merged.

MODERATOR
Nov 19 '08 #5
Plater
7,872 Expert 4TB
I know your pain. I ran into this too.
You have to use win32_API to destroy hIcon references

Here it what I was doing, you might be able to modify it to fit your purposes
Expand|Select|Wrap|Line Numbers
  1. [System.Runtime.InteropServices.DllImport("User32.dll")]
  2. private static extern bool DestroyIcon(IntPtr hIcon);
  3.  
  4. private Icon ImageToIcon(Image image)
  5. {
  6.    IntPtr ip= ((Bitmap)image).GetHicon();
  7.    Icon i = (Icon)Icon.FromHandle(ip).Clone();
  8.    DestroyIcon(ip);
  9.    return i;
  10. }
  11.  
  12. //then you can use the Icon.Destroy() where you are done with it
  13.  
Nov 19 '08 #6
Isn't there another way to release memory of icon rather than to call
Unmanaged
Expand|Select|Wrap|Line Numbers
  1. DestroyIcon(hicon);
Expand|Select|Wrap|Line Numbers
  1. icon.Dispose(); 
is not working anyway
Nov 20 '08 #7
Plater
7,872 Expert 4TB
Isn't there another way to release memory of icon rather than to call
Unmanaged
Expand|Select|Wrap|Line Numbers
  1. DestroyIcon(hicon);
Expand|Select|Wrap|Line Numbers
  1. icon.Dispose(); 
is not working anyway
Note my line about also calling:
Icon.Destroy()
Nov 20 '08 #8
As we are creating Icon again and not destroying it
Expand|Select|Wrap|Line Numbers
  1. Icon i = (Icon)Icon.FromHandle(ip).Clone();
Doesn't it cause another leak?
Nov 25 '08 #9
Plater
7,872 Expert 4TB
@prpradip
Thus the win32_API I call to destroy the icon pointed to by "ip"
Nov 25 '08 #10
Then doesn't it destroy new created Icon 'i' also?
Nov 26 '08 #11
Plater
7,872 Expert 4TB
No because you have made a clone of it into the Icon i
Nov 26 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: frustrated | last post by:
Before I begin, I must first make the following disclaimer: Although I have considerable programming experience, I do not consider myself by any means to be an expert C++ programmer. The following...
2
by: Sambucus | last post by:
Hi group! I am using C++ and java with JNI to get some text in a RICHEDIT to my java program. I do so by accessing a C++ method every second. It all works fine except that it leaks memory every...
7
by: andylcx | last post by:
hi all: I have a code like below, is there any serious memory leakage in my code. I am confusion now but no idea how to fix it. In the member function of class A, I create a new object of class B...
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
18
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
1
by: Gaël | last post by:
Hi everybody! I have a really big problem with ASP.NET application. I noticed that the w3wp.exe memory size, increase with the time and the use of my website. When it raise a certain value, w3wp...
0
by: kiran kumar | last post by:
Hi All, I am working on embedded python on C these days. I feel there is a memory leakage in this code. I have used our own memory pool and all the python code will use the heap from this memory...
14
by: madhawi | last post by:
i want to know that on what situation memory leakage happan and what is the solution to solve the problem of memory leakage.
3
by: Godzilla | last post by:
Hello, I have a program that create and pop an object off a queue, but it is experiencing some memory leakage. I have been unable to detect where the memory leakage occur. The strange thing is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.