- what if shmalloc fails
- Posted by Martijn on October 14th, 2003
Hi,
I have a PIDL library that works with PIDLs as regular memory, meaning it
free's them using the regular "free" (the complement to "malloc" and it's
friends).
I use ParseDisplayName to get the PIDL to a specified path. This function
allocates memory to store the PIDL. Here are my questions:
1) is it essential to use the SHMalloc interface vs the standard free?
2) and if not, what happens if SHGetMalloc fails - how will I be able to
free the memory?
Thanks for any help!
--
Martijn
http://www.sereneconcepts.nl
- Posted by Stephen Kellett on October 14th, 2003
In message <3f8bc511$0$58715$e4fe514c@news.xs4all.nl>, Martijn
<subscription-NOSPAM-101@hotNOFILTERmail.com> writes
In general, with any memory allocation interface, you should use the
appropriate deallocator with the appropriate allocator. If the
allocating code uses new, you use delete. If it uses malloc, you use
free, if it uses CoTaskMemAlloc, you use CoTaskMemFree, if it used
netApiBufferAllocate you use netApiBufferFree and so on.
If your code is allocating via SHMalloc, you should deallocate via the
same mechanism.
The reason for this is portability - what if the internal implementation
of SHMalloc is changed in a future version of the OS (even on the same
processor). If you used the correct functions, all works well. However
if you used (free) and the internal implementation used CoTaskMemAlloc /
CoTaskMemFree then your application will no longer work correctly.
Why would it fail? If it fails to allocate then you have nothing to
deallocate.
Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
- Posted by Martijn on October 14th, 2003
Thanks for your reply. I guess you must have misread the original post.
I didn't allocate the memory, ParseDisplayName did.
I didn't allocate the memory, ParseDisplayName did.
Point is that I don't even know _how_ the memory was allocated (it doesn't
specify this anywhere in the documentation, but as this functions concerns
the shell, I assume it is allocated using the IMalloc interface), nor do I
have a handle to the IMalloc interface at the time it allocates this memory.
--
Martijn
http://www.sereneconcepts.nl
- Posted by Stephen Kellett on October 14th, 2003
In message <3f8bef8a$0$58702$e4fe514c@news.xs4all.nl>, Martijn
<subscription-NOSPAM-101@hotNOFILTERmail.com> writes
If allocated using IMalloc, its a safe bet (unless someone else knows
otherwise) to use CoTaskMemFree to deallocate the memory. If you read
various bits of the documentation, documentation for this type of thing
now tends to prefer CoTaskMemAlloc/Realloc/Free.
(Sorry for being vague, this machine doesn't have MSDN on it, and the
one next to it refuses to start help - just goes into a loop when I
click help, so I can't find the reference I want.
Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
- Posted by Alex Blekhman on October 14th, 2003
"Martijn" <subscription-NOSPAM-101@hotNOFILTERmail.com> wrote in message
news:3f8bef8a$0$58702$e4fe514c@news.xs4all.nl...
Yes, you should use IMalloc interface. Look here for example code:
"Managing the File System"
<http://msdn.microsoft.com/library/en...l/programmersg
uide/shell_basics/shell_basics_programming/manage.asp?frame=true#example>.
- Posted by Lucian Wischik on October 14th, 2003
Alex Blekhman wrote:
The original poster already knows that! What was asked was more
subtle:
(1) you call a function like ParseDisplayName. This returns a pointer.
(2) The docs tell you to free the pointer by calling SHGetMalloc() to
get an IMalloc, and then to free the pointer using this IMalloc.
(3) *** BUT *** what happens if SHGetMalloc fails? Then you're left
with a pointer that you can't free!
Stephen suggested a feature that I think is undocumented in this
situation: using CoTaskMemFree. Another possibility I see is to call
SHGetMalloc first, and only ParseDisplayName if you have a valid
IMalloc to free it with later.
--
Lucian
- Posted by Raymond Chen on October 14th, 2003
SHGetMalloc is now deprecated. I'll have the docs updated. It's
just a wrapper around CoGetMalloc, which in turn is the same
allocator as CoTaskMemAlloc. This is alluded to in the comments
// Notes:
// In next version of Windowso release, SHGetMalloc will be
replaced by
// the following macro.
//
// #define SHGetMalloc(ppmem) CoGetMalloc(MEMCTX_TASK, ppmem)
//
The rule of theumb is that any memory whose ownership crosses a
COM boundary must be allocated via CoTaskMemAlloc, unless
explicitly documented otherwise.