Memory Leakage Due to ICON

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prpradip
    New Member
    • Nov 2008
    • 28

    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.
  • prpradip
    New Member
    • Nov 2008
    • 28

    #2
    Memory Leakage Due to ICON

    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.

    Code:
    private void ChildForm_Load(object sender, EventArgs e)
            {
                Random r = new Random();
                for (int i = 0; i < 4000; i++)
                {
                    int index = r.Next(1, 6);
                    using (SysImageList sil = new SysImageList()) //SysImageList is class containing function to get Icon.
                    {
                        System.Drawing.Icon ico = sil.Icon(index); //SysImagelist.Icon(int i) is the function to get the Icon using index.
                        if (ico != null)
                        {
                            imageList1.Images.Add(ico);
                        }
                    }
                }
            }
    
    [DllImport("comctl32.dll", CharSet = CharSet.Auto)]
     public static extern bool ImageList_Destroy(IntPtr hImageList);
    -------------
    protected override void Dispose(bool disposing)
            {
    		
    	    bool destroyImgList = ImageList_Destroy(imageList1.Handle); //Returning always true.
    
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }


    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.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      ImageList.Image s gives you all the images ...

      Comment

      • prpradip
        New Member
        • Nov 2008
        • 28

        #4
        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.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          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

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            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
            [code=c#]
            [System.Runtime. InteropServices .DllImport("Use r32.dll")]
            private static extern bool DestroyIcon(Int Ptr hIcon);

            private Icon ImageToIcon(Ima ge image)
            {
            IntPtr ip= ((Bitmap)image) .GetHicon();
            Icon i = (Icon)Icon.From Handle(ip).Clon e();
            DestroyIcon(ip) ;
            return i;
            }

            //then you can use the Icon.Destroy() where you are done with it
            [/code]

            Comment

            • prpradip
              New Member
              • Nov 2008
              • 28

              #7
              Isn't there another way to release memory of icon rather than to call
              Unmanaged
              Code:
              DestroyIcon(hicon);
              Code:
              icon.Dispose();
              is not working anyway

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Originally posted by prpradip
                Isn't there another way to release memory of icon rather than to call
                Unmanaged
                Code:
                DestroyIcon(hicon);
                Code:
                icon.Dispose();
                is not working anyway
                Note my line about also calling:
                Icon.Destroy()

                Comment

                • prpradip
                  New Member
                  • Nov 2008
                  • 28

                  #9
                  As we are creating Icon again and not destroying it
                  Code:
                  Icon i = (Icon)Icon.FromHandle(ip).Clone();
                  Doesn't it cause another leak?

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Originally posted by prpradip
                    As we are creating Icon again and not destroying it
                    Code:
                    Icon i = (Icon)Icon.FromHandle(ip).Clone();
                    Doesn't it cause another leak?
                    Thus the win32_API I call to destroy the icon pointed to by "ip"

                    Comment

                    • prpradip
                      New Member
                      • Nov 2008
                      • 28

                      #11
                      Then doesn't it destroy new created Icon 'i' also?

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        No because you have made a clone of it into the Icon i

                        Comment

                        Working...