2

I use ShBrowseForFolder() to select a working folder in an application to launch Perl scripts that I am writing. Once ShGetPathFromIDList() has given me the text representation of the chosen folder, the documentation says to call IMalloc::Free to release its PIDL. However, the MSDN page describing this shell function says it is heavily deprecated. Instead it recommends CoTaskMemFree() as it is a more modern approach and simpler to use.

Is it safe to use CoTaskMemFree() to deallocate PIDLs?

1

1 Answer 1

1

The Windows SHBrowseForFolder documentation says:

It is the responsibility of the calling application to call CoTaskMemFree to free the IDList returned by SHBrowseForFolder when it is no longer needed.

On the other hand, the Windows Embedded Compact SHBrowseForFolder documentation says:

The calling application is responsible for freeing the returned PIDL with the IMalloc::Free method for the shell allocator. To get a handle to the IMalloc:IUnknown interface for the shell's allocator, call SHGetMalloc.

Now, SHGetMalloc is deprecated on Windows but not on Compact.

Bottom line: Assuming that you're developing for Windows, go ahead and use CoTaskMemFree.

4
  • Yeah - they do talk at cross purposes and I don't find the new MSDN as convenient or easy to use as the one in Visual Studios past. I find it VERY hard to turn up specific information or browse generally. I also take exception to the idea MS are pushing that ShBrowseForFolder() should be replaced by the standard file open dialogue with only folders enabled. That to my mind is counter-intuitive and ugly. The real ShBrowseForFolder() dialogue is far more compact, fit to purpose and visually appealing - which is why I use it!
    – user3259248
    Aug 10, 2014 at 5:33
  • From SHGetMalloc: "If you find an older reference document that suggests or even requires the use of SHGetMalloc, it is acceptable and encouraged to use CoTaskMemAlloc and CoTaskMemFree instead." Aug 10, 2014 at 15:45
  • @IInspectable To be clear, that's the SHGetMalloc documentation for Windows. Aug 10, 2014 at 18:34
  • I would assume that the information linked to by @RaymondChen above is also true for Windows Compact 2013. If the application/shell extension links against ole32.lib it should then be safe to call CoTaskMemFree, although I don't have the shlobj.h header file for Windows Compact 2013 to see if it contains the same comment. Aug 10, 2014 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.