The MASM Forum

General => The Campus => Topic started by: K_F on November 07, 2014, 07:31:36 PM

Title: GetProcessHeap ?
Post by: K_F on November 07, 2014, 07:31:36 PM
Maybe I'm blind but I cannot see (MSDN) where to de-allocate the heap handle returned by the GetProcessHeap function.
MSDN says that you shouldn't use HeapFree, HeapDestroy..etc.. but doesn't say what you can use  :icon_eek:

I'm placing this in the WinProc's WM_Close message...

Maybe the process termination does this automatically ??
Title: Re: GetProcessHeap ?
Post by: Gunther on November 07, 2014, 07:38:42 PM
Hi Tony,

Quote from: K_F on November 07, 2014, 07:31:36 PM
Maybe the process termination does this automatically ??

yes it does by the termination of the process, I think. I couldn't find nothing other as you found.

Gunther
Title: Re: GetProcessHeap ?
Post by: Zen on November 08, 2014, 06:07:41 AM
VAN/K_F,
Each process has at least one heap: the default process heap. The default heap is created at process startup and is never deleted during the process's lifetime.
Look at the example mentioned at the bottom of the GetProcessHeap documentation: Getting Process Heaps (http://msdn.microsoft.com/en-us/library/windows/desktop/ee175820(v=vs.85).aspx).
They just completely ignore the HANDLE value. I don't think it makes any difference,...but, I just set the HANDLE value equal to zero when I'm finished allocating and using memory from the Default Process Heap.
...You could, of course, call CloseHandle (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx), but the documentation doesn't list the Default Process Heap as an object that CloseHandle will close the handle to. In general, CloseHandle invalidates the specified object handle, decrements the object's handle count, and performs object retention checks. After the last handle to an object is closed, the object is removed from the system.
It seems that all the documentation that I've read will recommend calling CloseHandle on a handle that is returned from a Windows API, if it is necessary to decrement the reference count on the handle.
Heap Functions, MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/aa366711(v=vs.85).aspx)
Title: Re: GetProcessHeap ?
Post by: K_F on November 08, 2014, 08:40:02 AM
Ja, I follow what you're saying, which is why I thought it was destroyed on the process termination.

The thing that made me think was that, if every process had a default heap, why would GetProcessHeap return a NULL, if it's only a handle.
:biggrin:
Title: Re: GetProcessHeap ?
Post by: Zen on November 08, 2014, 09:51:42 AM
VAN/K_F,   
Quote from: VANThe thing that made me think was that, if every process had a default heap, why would GetProcessHeap return a NULL, if it's only a handle.
Hell if I know,...I've had that happen on occasion too,...It seems like one of those functions that would never fail,...:dazzled:
...By the way,...that first line of my post:
Each process has at least one heap: the default process heap. The default heap is created at process startup and is never deleted during the process's lifetime. is quoted from Windows Internals. The author never mentions the HANDLE value returned from GetProcessHeap.
Title: Re: GetProcessHeap ?
Post by: K_F on November 08, 2014, 10:29:23 AM
Thanks.. I sort of gathered that much.
One could deduce that GetProcessHeap.. is the default heap handle, and we have no control over, except for using for HeapAlloc functions, etc..
:t

Title: Re: GetProcessHeap ?
Post by: TouEnMasm on November 08, 2014, 06:19:01 PM
QuoteOne could deduce that GetProcessHeap.. is the default heap handle
And it is called like that by MSDN  (search words:Msdn Heap memory)
http://msdn.microsoft.com/en-us/library/ms810603.aspx (http://msdn.microsoft.com/en-us/library/ms810603.aspx)
Title: Re: GetProcessHeap ?
Post by: Tedd on November 12, 2014, 06:01:38 AM
GetProcessHeap returns the heap (that has already been allocated) for the process -- it doesn't cause a new one to be allocated.
Since you didn't allocate it (or cause it to be allocated on your behalf), you don't need to free it (and can't.)