Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

heapfree vs localfree

565 views
Skip to first unread message

RG

unread,
Jul 6, 2006, 3:22:47 PM7/6/06
to
If I declare something like

LPTSTR ReferencedDomain=NULL;

to clear the memory block, do these two functions do the same thing? Are
they interchangeable?

HeapFree(GetProcessHeap(), 0, ReferencedDomain);
LocalFree(ReferencedDomain);

msdn says to use the heap functions because they are faster but besides
that, are there any other advantages?


Igor Tandetnik

unread,
Jul 6, 2006, 3:39:15 PM7/6/06
to
RG <ryan_g...@hotmail.com.(remove.this)> wrote:
> Are they interchangeable?
>
> HeapFree(GetProcessHeap(), 0, ReferencedDomain);
> LocalFree(ReferencedDomain);

No. HeapFree deallocates memory allocated with HeapAlloc. LocalFree
deallocates memory allocated with LocalAlloc.

LocalAlloc et al are retained for backward compatibility and should not
be used in new code.

Note also that your example won't compile. LocalFree takes HLOCAL handle
as a parameter, rather than a pointer.

Why do you have to go through Win32 API for your memory management? You
may find that 'new' (in C++) or malloc (in C) are more convenient.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Heinz Ozwirk

unread,
Jul 6, 2006, 4:23:38 PM7/6/06
to
"RG" <ryan_g...@hotmail.com.(remove.this)> schrieb im Newsbeitrag
news:u0OwRGTo...@TK2MSFTNGP04.phx.gbl...

Use HeapFree to free memory allocated with HeapAlloc or HeapReAlloc. Use
LocalFree to free memory allocated with LocalAlloc. So no, they are not
interchangeable.

Heinz

RG

unread,
Jul 7, 2006, 9:04:39 AM7/7/06
to
Thanks for the input but maybe I wasn't clear enough. While referencing
security APIs on msdn I had found certain
articles,http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthz/security/convertsidtostringsid.asp,
that said to use LocalFree to clear memory assigned to pointers. So all
that is happening is initialize the pointer, pass it to the API, call
LocalFree when done. There is no call to LocalAlloc, HeapAlloc, new or
delete. For another example, take a look here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthz/security/taking_object_ownership_in_c__.asp

However, the LocalFree documentation says to use the Heap functions and I've
seen people use things like HeapFree(GetProcessHeap(), 0, ReferencedDomain);

So now the questions I ask myself are what is the most appropriate way to
clear the assigned memory block? and if I am NOT using LocalAlloc or
HeapAlloc, which one do I use and is there a difference between the two?

p.s. the example given in the previous post compiles and runs without error

"Igor Tandetnik" <itand...@mvps.org> wrote in message
news:uutHdkTo...@TK2MSFTNGP03.phx.gbl...

William DePalo [MVP VC++]

unread,
Jul 7, 2006, 10:05:46 AM7/7/06
to
"RG" <ryan_g...@hotmail.com.(remove.this)> wrote in message
news:uU$HrXcoG...@TK2MSFTNGP03.phx.gbl...

> Thanks for the input but maybe I wasn't clear enough. While referencing
> security APIs on msdn I had found certain
> articles,http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthz/security/convertsidtostringsid.asp,
> that said to use LocalFree to clear memory assigned to pointers. So all
> that is happening is initialize the pointer, pass it to the API, call
> LocalFree when done. There is no call to LocalAlloc, HeapAlloc, new or
> delete. For another example, take a look here:

First, Igor's reply is spot on. Allocator and deallocator functions always
come in pairs. You can't mix and match them.

Second, LocalAlloc() goes way back. It's use is generally deprecated.

Third, operating system vendors often find themselves stuck between a rock
and a hard place. They can deprecate the use of a function, but if they
retire support for it then old code will break.

Fourth, and this is the crux of the issue, SetEntriesInAcl() returns a
pointer to a buffer that was allocated by means of LocalAlloc(). The docs
say you must use LocalFree() to free it. The operating system is always
right. :-)

It has done this forever. It likely won't change anytime soon. The fact that
it still uses the old disfavored allocator is one of those "grin and bear
it" facts of life. In fairness, any performance penalty should be
negligible.

Regards,
Will


RG

unread,
Jul 7, 2006, 10:35:14 AM7/7/06
to

"William DePalo [MVP VC++]" <willd....@mvps.org> wrote in message
news:u3rPy5co...@TK2MSFTNGP04.phx.gbl...

>
> Fourth, and this is the crux of the issue, SetEntriesInAcl() returns a
> pointer to a buffer that was allocated by means of LocalAlloc(). The docs
> say you must use LocalFree() to free it. The operating system is always
> right. :-)

OK makes more sense now that someone pointed out (the obvious fact) that
SetEntriesInAcl used LocalAlloc

Thx everyone for clearing this up.
Ryan


0 new messages