Who calls DestroyIcon after an ExtractIcon?

  • Thread starter Thread starter Vagabond Software
  • Start date Start date
V

Vagabond Software

I have an application that will consume an IconTool class, which makes a
call to the ExtractIcon API function. The documentation for the ExtractIcon
function makes the following remarks:

"You must destroy the icon handle returned by ExtractIcon by calling the
DestroyIcon function."

I know that if I call DestroyIcon within the IconTool class, I get a "cannot
access disposed object" error message. So, is there a way to make sure the
icon handle is disposed or do I have to leave it up to the consumer?

Any advice or suggestions are greatly appreciated.

Carl
 
Herfried K. Wagner said:
What's the 'IconTool' class?

--

It is a custom class that I have written as part of a larger custom library.
The ExtractIcon wrapper in the IconTool class returns an Icon type, which
could be consumed by assigning it the Icon property of a Windows Form, for
example.

Here is a code snippet:

Public Function ExtractIcon(ByVal iconIndex As Integer) As Icon

Dim hndIcon As Integer
Dim iconEx As Icon
Dim pathBuilder As New StringBuilder(MAX_STRING_LENGTH,
MAX_STRING_LENGTH)
Dim processId As Integer
Dim iconFileName As String

'Gather the information necessary to extract the desired icon from the
'"shell32.dll" file and pass the arguments to the ExtractIcon method.
processId = System.Diagnostics.Process.GetCurrentProcess.Handle.ToInt32
pathBuilder.Append(Environment.GetFolderPath(Environment.SpecialFolder.System))
pathBuilder.Append("\shell32.dll")
iconFileName = pathBuilder.ToString

hndIcon = Me.ExtractIcon(processId, iconFileName, iconIndex)
If hndIcon > 0 Then
iconEx = Icon.FromHandle(New IntPtr(hndIcon))
'DestroyIcon(hndIcon)
End If

Return iconEx
End Function

I'm still feeling my around the issues of scope and garbage collection. As
you can see, the DestroyIcon call is commented out for what are now obvious
reasons. I would still like to be able to ensure that memory used in this
process is properly cleaned up. Do I need DestroyIcon to do that? If so,
is that the responsibility of the consumer or can I do it within the
IconTool class?

Carl
 
iconEx = Icon.FromHandle(New IntPtr(hndIcon))

If you make this

iconEx = Icon.FromHandle(New IntPtr(hndIcon)).Clone()

you should be able to destroy the icon handle afterwards.


Mattias
 
Mattias Sjögren said:
If you make this

iconEx = Icon.FromHandle(New IntPtr(hndIcon)).Clone()

you should be able to destroy the icon handle afterwards.


Mattias

--

That did the trick. Thanks.

Carl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top