Does a call to HeapAlloc with Assembly require deallocation?

258 Views Asked by At

At the beginning of my program I allocate memory using HeapAlloc. Is it neccessary to deallocate it or is that done by the system, when the program ends?

start:
    call   GetProcessHeap
    mov    r11, rax                ; r11 contains handle

    mov    rdi, 8000000
    mov    rsi, 0
    mov    rdx, r11
    call   HeapAlloc        

    mov    r12, rax       ; r12 contains pointer to memory block

    mov    ecx, 1000000
    xor    eax, eax
.looptop_populate
    add    rax, rcx
    mov    [r12+8*rcx-8], rax
    loop   .looptop_populate

    mov    rdi, [r12]
    call   write_uinteger
    xor    eax, eax
    ret

; goasm /x64 /l malloc
; golink /console malloc.obj kernel32.dll

At the moment the memory seems to be deallocated automatically, but it is good style to just ignore the deallocation?

1

There are 1 best solutions below

1
On

At the beginning of my program I allocate memory using HeapAlloc. Is it neccessary to deallocate it or is that done by the system, when the program ends?

What you've allocated is part of the running process memory space. That ceases to exist when the process terminates.

At the moment the memory seems to be deallocated automatically, but it is good style to just ignore the deallocation?

That's correct. When a process terminates, its address spaces no longer exists. There is no way it could stay allocated. Generally, it's not considered good style to ignore the deallocation because it makes the code unusable for larger programs and it makes it harder to debug memory leaks. But it won't actually cause anything to leak after the process terminates.