Is it nessessary to free up HICONs obtained from other HWNDs via ->
hIcon=(HICON)::SendMessage(m_hWnd,WM_GETICON,ICON_BIG,0);
or
hIcon=::CopyIcon((HICON)::GetClassLong(m_hWnd,GCL_HICON));
My application makes hundreds of these calls without calling
DestroyIcon() on any of the HICONs...They are just copies of icons
from other modules right? There is no need to release any of
them...am I correct in assuming this?
Mike L.G.
I took this from the MSDN help entry for CopyIcon():
<quote>
The CopyIcon function enables an application or dynamic-link library (DLL)
to get its own handle to an icon owned by another module. If the other
module is freed, the application icon will still be able to use the icon.
Before closing, an application must call the DestroyIcon function to free
any system resources associated with the icon.
</quote>
The way I read that, the lifetime of the copy is quite independent of the
original, so yes, you do have to free the copy.
> My application makes hundreds of these calls without calling
> DestroyIcon() on any of the HICONs...They are just copies of icons
> from other modules right? There is no need to release any of
> them...am I correct in assuming this?
I have never sent the WM_GETICON message across a process boundary so I
don't know for sure. My _guess_ is that you don't have to free icons whose
handles you obtain this way. Of course, I could be wrong.
Regards,
Will
Hello Will,
I believe you are right...now that I think about it, I remember I had
tucked away a DestroyIcon() in a destructor somewhere...all the HWNDs
HICONs that I had obtained via SendMessage(hWnd,WM_GETICON,ICON_BIG,0)
had their icons erased at the upper left corner of their application
windows...not what I had wanted in the slightest. If you had to deal
with a great many window HICONs...would you go the CopyIcon() or the
WM_GETICON route? I could see that CopyIcon() would be
safer...dealing with a copy rather then the actual icon...but it would
seem like a waste of space when I could just deal with the HICON
directly via a SendMessage() call...I would just have to be careful
not to call DestroyIcon() anywhere. Any advice would be appreciated.
Thanks again Will.
Mike L.G.
I'm not sure about this but you may not have a choice. If you are "stealing"
icon handles from a window and if that window is destroyed or if the owning
application exits where does that leave you? Are you tracking the lifetime
of windows or applications? If not, what happens to the icon whose handle
when the owner is gone? I would think that the copy option is the safest if
you don't have any knowledge of the windows whose icons your are retrieving.
Of course, this is only speculation on my part. YMMV.
Regards,
Will