1

From MSDN about IShellItemImageFactory::GetImage:

It is the responsibility of the caller to free this retrieved resource through DeleteObject when it is no longer needed.

My question is:

I'm using the IntPtr in an IShellItemImageFactory.GetImage call where the IShellItemImageFactory is decorated with [ComImportAttribute()]. Do I still need to call DeleteObject, or will that cause trouble when the CLR(?) will try deleting it as part of the Factory?

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • @HansPassant I don't understand your comment. That is exactly what I quoted. My question is what happens when using `ComImportAttribute` - does that make it a bad thing to use DeleteObject or does it not change anything. – ispiro Feb 06 '14 at 20:28
  • you might want to check out the [Windows API Code Pack](http://archive.msdn.microsoft.com/WindowsAPICodePack) or the now nugeted version https://www.nuget.org/packages/Windows7APICodePack/ – Mgetz Feb 06 '14 at 20:28
  • @Mgetz Thanks. But that's what I've been using until now, and is exactly what I'm trying to avoid - using all of that for a little bit of it. – ispiro Feb 06 '14 at 20:57

1 Answers1

3

The CLR will never automatically release an IntPtr. It cannot possibly know what specific function needs to be called to release the pointer. It is entirely up to you to pinvoke DeleteObject().

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • OK. Thanks. (I was actually asking this mainly because of another answer of yours [here](http://stackoverflow.com/questions/9210723/do-i-need-to-free-resources-when-calling-shcreateitemfromparsingname-from-manage/9211291#9211291) where you state: `it is automatic, just like .NET objects.` and say that Disposing might be `a good way to shoot your foot though`. I thought this will apply here as well. Thanks again. – ispiro Feb 06 '14 at 20:34
  • That answer was about a COM interface pointer, not an IntPtr. The CLR knows a lot about interface pointers, starting by it automatically creating an RCW. You cannot compare the two. – Hans Passant Feb 06 '14 at 20:45
  • Sorry for continuing , but the other answer here says I not only have to **DeleteObject** the `IntPtr`, but also **FinalReleaseCOMObject** the `IShellItemImageFactory`. I understand from your other answer that at least that should _not_ be done. Which one is it? – ispiro Feb 06 '14 at 20:54
  • OK. Never mind. I see he now deleted his answer. Thanks again. – ispiro Feb 06 '14 at 21:00