News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Heap APIs

Started by shankle, August 28, 2014, 08:00:24 AM

Previous topic - Next topic

shankle


                             8-27-2014
                     
My 1st time using the Heap Marcos
Heapcreate seems to work but HeapAlloc does not even show either
error message below.
I am trying to create a growing heap.
Suggestion would be appreciated

HeapH   dd     0

    invoke HeapCreate, HEAP_GENERATE_EXCEPTIONS,10000,0
    mov [HeapH],eax

; test begin code   
;tmsg14    db  'HeapCreate seems to work',0
;tmsg15    db  'HeapCreate failed',0
   cmp eax,0h  ;if NuLL, HeapAlloc failed
    jne >>.tout
   invoke MessageBox, [hWnd],addr tmsg15,addr tmsg15,MB_OK ;true
   jmp >>.tout2
.tout
   invoke MessageBox, [hWnd],addr tmsg14,addr tmsg14,MB_OK ;true
.tout2 
; test end codecode

    invoke HeapAlloc, [HeapH],HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY,10000

; test begin code   
;tmsg10    db  'HeapAlloc seems to work',0
;tmsg11    db  'HeapAlloc failed',0
   cmp eax,0h  ;if NuLL, HeapAlloc failed
    jne >>.tout3
   invoke MessageBox, [hWnd],addr tmsg11,addr tmsg11,MB_OK ;true
   jmp >>.tout4
.tout3
   invoke MessageBox, [hWnd],addr tmsg10,addr tmsg10,MB_OK ;true
.tout4 ; test end code

dedndave

there should be no need to create a heap
i generally use GetProcessHeap to get the system-created "default process" heap

if you successfully allocate a heap block, you should free it when done, even for a test piece
INVOKE  HeapFree,hHeap,lpBlock
lpBlock being the address returned by HeapAlloc

because you have specified HEAP_GENERATE_EXCEPTIONS, there is no need to test for success
if it is successful, there will be a block pointer in EAX
otherwise, it will generate an exception, and the program will terminate (possibly opening the debugger)

dedndave

here is a piece using MASM syntax

    .DATA?

hHeap   HANDLE ?
lpBlock LPVOID ?

    .CODE

Start:
    INVOKE  GetProcessHeap
    mov     hHeap,eax

    INVOKE  HeapAlloc,hHeap,HEAP_GENERATE_EXCEPTIONS or HEAP_ZERO_MEMORY,10000
    mov     lpBlock,eax

    INVOKE  HeapFree,hHeap,NULL,lpBlock

Yuri

Quote from: shankle on August 28, 2014, 08:00:24 AM
Heapcreate seems to work but HeapAlloc does not even show either
error message below
Both work for me in your code. I see tmsg10.

shankle

Thanks guys for responding.
To Yuri:
I checked my code again and recompiled the program.
Same results. HeapAlloc shows nothing.
Then the dreaded C000005 error.............
This is a 64-bit program I am trying to convert from MASM32.

qWord

Handles are pointer types and these are 64 bit width for Windows x86-64.
MREAL macros - when you need floating point arithmetic while assembling!

shankle

Changing HeapH to "dq" fixed the problem but opened up another can of worms
in that it will involve several hundred changes for "PtrMem and ecx".
I will make the changes and hope for the best.

dedndave

i didn't think about 64-bit - i was thinking 32-bit   :P
the heap handle AND the pointer returned by HeapAlloc are both 64-bits wide