Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Please give me some advice on the proper handling of HICONS

Please give me some advice on the proper handling of HICONS

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomgraphicslearning
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jase Jennings
    wrote on last edited by
    #1

    Hi, I'm trying to track down some GDi leakage in my application and think i may be using hIcon incorrectly. At what point does a hIcon resource get released ? I have a dialog which sets ups some icon buttons during OnInitDialog() like this : [b] HICON hIcon; hIcon = (HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_STOP), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE); m_BtnStop.SetIcon(hIcon); hIcon = (HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_REWIND), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE); m_BtnRewind.SetIcon(hIcon); hIcon = (HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_FORWARD), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE); m_BtnForward.SetIcon(hIcon); hIcon = (HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, LR_DEFAULTSIZE); m_BtnPanel.SetIcon(hIcon); [/b] Question 1. ----------- When this function is run over and over (dialog is destroyed then reopened), am i leaking icon resources ? I suspect that i need to have a HICON for each of the buttons, and in the dialogs destructor (or some other function - please tell me which) i need to DestroyIcon(). Is this correct ? Question 2. ----------- I guess this is really answered by question 1, but if i change the icon for a button dynamically (i.e. depending upon the state of my app, the button uses different icons), should i use DestroyIcon() before using SetIcon() with new icon definition ? Any other tips you can give me regarding icons ? Also, is there a tool i can use to see GDI resources so i can see if they're leaking ? Jase ------------------------------------------------------------------------------------------------------------------------------------------------------------------- View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com

    PJ ArendsP 1 Reply Last reply
    0
    • J Jase Jennings

      Hi, I'm trying to track down some GDi leakage in my application and think i may be using hIcon incorrectly. At what point does a hIcon resource get released ? I have a dialog which sets ups some icon buttons during OnInitDialog() like this : [b] HICON hIcon; hIcon = (HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_STOP), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE); m_BtnStop.SetIcon(hIcon); hIcon = (HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_REWIND), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE); m_BtnRewind.SetIcon(hIcon); hIcon = (HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_FORWARD), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE); m_BtnForward.SetIcon(hIcon); hIcon = (HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, LR_DEFAULTSIZE); m_BtnPanel.SetIcon(hIcon); [/b] Question 1. ----------- When this function is run over and over (dialog is destroyed then reopened), am i leaking icon resources ? I suspect that i need to have a HICON for each of the buttons, and in the dialogs destructor (or some other function - please tell me which) i need to DestroyIcon(). Is this correct ? Question 2. ----------- I guess this is really answered by question 1, but if i change the icon for a button dynamically (i.e. depending upon the state of my app, the button uses different icons), should i use DestroyIcon() before using SetIcon() with new icon definition ? Any other tips you can give me regarding icons ? Also, is there a tool i can use to see GDI resources so i can see if they're leaking ? Jase ------------------------------------------------------------------------------------------------------------------------------------------------------------------- View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com

      PJ ArendsP Offline
      PJ ArendsP Offline
      PJ Arends
      wrote on last edited by
      #2

      1. Yes you do have to call DestroyIcon() 2. that depends, if you are going to reuse the icon, keep it around until your dialog is destroyed, that way you don't have to constantly reload it. If you are finished with it then call DestroyIcon(). As for monitoring resource leaks, I use Windows Resource meter (RSRCMTR.EXE on Win98 - I assume other versions of windows have the program (or something similar), there may be better programs around, but I have this one). --- Blessed are those who can laugh at themselves, for they shall never cease to be amused :laugh:

      Within you lies the power for good; Use it!

      J 2 Replies Last reply
      0
      • PJ ArendsP PJ Arends

        1. Yes you do have to call DestroyIcon() 2. that depends, if you are going to reuse the icon, keep it around until your dialog is destroyed, that way you don't have to constantly reload it. If you are finished with it then call DestroyIcon(). As for monitoring resource leaks, I use Windows Resource meter (RSRCMTR.EXE on Win98 - I assume other versions of windows have the program (or something similar), there may be better programs around, but I have this one). --- Blessed are those who can laugh at themselves, for they shall never cease to be amused :laugh:

        J Offline
        J Offline
        Jase Jennings
        wrote on last edited by
        #3

        Thanks for your reply. Would you suggest loading my icon resource in OnCreate and destrying them in OnDestroy() then ? Those are my current thought on this based on your reply. Thanks also for telling me aboutr RSRCMTR.EXE . I didn't know I had this program! Jase ------------------------------------------------------------------------------------------------------------------------------------------------------------------- View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com

        1 Reply Last reply
        0
        • PJ ArendsP PJ Arends

          1. Yes you do have to call DestroyIcon() 2. that depends, if you are going to reuse the icon, keep it around until your dialog is destroyed, that way you don't have to constantly reload it. If you are finished with it then call DestroyIcon(). As for monitoring resource leaks, I use Windows Resource meter (RSRCMTR.EXE on Win98 - I assume other versions of windows have the program (or something similar), there may be better programs around, but I have this one). --- Blessed are those who can laugh at themselves, for they shall never cease to be amused :laugh:

          J Offline
          J Offline
          Jase Jennings
          wrote on last edited by
          #4

          Can you give me any advice on tracking GDI resource leaks ? My application leaks quite badly! I have dealt with the HICON issue now, creating in OnCreate and destroying in OnDestroy(). I was also using a class called CSXButton which leaks pretty badly too, so i've removed all traces of that and am now using just a CButton with icons. I also did use some text drawing functions on a splash screen which had leaks, but i resolved that by freeing my CFonts before changing them and started to use dc.save and dc.restore. I don't believe i am using any other gdi functions, but i am using several control classes sourced from codeguru and from here that may be leaking. How can i trace the source of the leak ? Please don't suggest a third party product that i have to pay for ... i want to become familar with some methods to track these problems myself. Jase ------------------------------------------------------------------------------------------------------------------------------------------------------------------- View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com

          PJ ArendsP S 2 Replies Last reply
          0
          • J Jase Jennings

            Can you give me any advice on tracking GDI resource leaks ? My application leaks quite badly! I have dealt with the HICON issue now, creating in OnCreate and destroying in OnDestroy(). I was also using a class called CSXButton which leaks pretty badly too, so i've removed all traces of that and am now using just a CButton with icons. I also did use some text drawing functions on a splash screen which had leaks, but i resolved that by freeing my CFonts before changing them and started to use dc.save and dc.restore. I don't believe i am using any other gdi functions, but i am using several control classes sourced from codeguru and from here that may be leaking. How can i trace the source of the leak ? Please don't suggest a third party product that i have to pay for ... i want to become familar with some methods to track these problems myself. Jase ------------------------------------------------------------------------------------------------------------------------------------------------------------------- View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com

            PJ ArendsP Offline
            PJ ArendsP Offline
            PJ Arends
            wrote on last edited by
            #5

            It is easy to forget (I forget quite often:( ), but you just have to make sure that everytime you load or create a resource you delete it when you are finished with it. If you look in MSDN at the functions that load/create a resource, there will be info on the function that is needed to delete it. for example, if you load a cursor with the ::LoadImage() function, you have to remove it with the ::DestroyCursor() function. The way I find leaks is to read the source code, and lots of trial and error, sorry I don't have a quick fix for you, but c'ést la vie. --- Blessed are those who can laugh at themselves, for they shall never cease to be amused :laugh:

            Within you lies the power for good; Use it!

            1 Reply Last reply
            0
            • J Jase Jennings

              Can you give me any advice on tracking GDI resource leaks ? My application leaks quite badly! I have dealt with the HICON issue now, creating in OnCreate and destroying in OnDestroy(). I was also using a class called CSXButton which leaks pretty badly too, so i've removed all traces of that and am now using just a CButton with icons. I also did use some text drawing functions on a splash screen which had leaks, but i resolved that by freeing my CFonts before changing them and started to use dc.save and dc.restore. I don't believe i am using any other gdi functions, but i am using several control classes sourced from codeguru and from here that may be leaking. How can i trace the source of the leak ? Please don't suggest a third party product that i have to pay for ... i want to become familar with some methods to track these problems myself. Jase ------------------------------------------------------------------------------------------------------------------------------------------------------------------- View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com

              S Offline
              S Offline
              Shog9 0
              wrote on last edited by
              #6

              Well, as the other guy mentioned, you always need to free everything you allocate. So, tracking resource usage at its simplest is mental; remember all the resources loaded by a class & make sure they're released before the class is destroyed, either by an OnDestroy() message handler (for a window class) or by the class destructor. If you've a lot of resources to keep track of, consider creating a separate manager class to deal with allocating and releasing them.    And now, i'm going to contradict myself ;P ... Looking at the example given in your original post, one easy solution suggests itself: use the LR_SHARED option in your call to LoadImage(). Unless you have multiple resources with the same ID, this will allow you to put the responsibility of managing these icons back on Windows. No need to free them, no need to worry about loading the same resource multiple times.    As for more advanced tools... I guess if you're using Win2000, you could watch the USER objects / GDI objects columns in the task manager over time... If you change your mind on commercial products, i'll recommend the one i use from time to time. :)

              J 1 Reply Last reply
              0
              • S Shog9 0

                Well, as the other guy mentioned, you always need to free everything you allocate. So, tracking resource usage at its simplest is mental; remember all the resources loaded by a class & make sure they're released before the class is destroyed, either by an OnDestroy() message handler (for a window class) or by the class destructor. If you've a lot of resources to keep track of, consider creating a separate manager class to deal with allocating and releasing them.    And now, i'm going to contradict myself ;P ... Looking at the example given in your original post, one easy solution suggests itself: use the LR_SHARED option in your call to LoadImage(). Unless you have multiple resources with the same ID, this will allow you to put the responsibility of managing these icons back on Windows. No need to free them, no need to worry about loading the same resource multiple times.    As for more advanced tools... I guess if you're using Win2000, you could watch the USER objects / GDI objects columns in the task manager over time... If you change your mind on commercial products, i'll recommend the one i use from time to time. :)

                J Offline
                J Offline
                Jase Jennings
                wrote on last edited by
                #7

                Hi, Thanks for your reply. So tell me then, which commercial product do you use and is there an evaluation version ? Jase ------------------------------------------------------------------------------------------------------------------------------------------------------------------- View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com

                S 1 Reply Last reply
                0
                • J Jase Jennings

                  Hi, Thanks for your reply. So tell me then, which commercial product do you use and is there an evaluation version ? Jase ------------------------------------------------------------------------------------------------------------------------------------------------------------------- View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com

                  S Offline
                  S Offline
                  Shog9 0
                  wrote on last edited by
                  #8

                  NuMega BoundsChecker; http://www.numega.com They do provide an evaluation version, though not as a direct download. Definately worth a try, as is their TrueTime profiling app.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups