0

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
jso
  • 11
  • 2

1 Answers1

4

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.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 1
    In modern (2000+) OS... in the historical ones it could have been real problem. ... keeping opened files is maybe still problem on some OS even today (winxp had this problem IIRC, not sure about later ones, didn't use them), so generally releasing/closing all allocated resources is good-taste style in programming, but specifically memory is handled well by anything common. – Ped7g Dec 28 '18 at 07:30