News:

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

Main Menu

HeapFree not reducing Process memory signature ?

Started by K_F, June 27, 2015, 07:54:23 PM

Previous topic - Next topic

K_F

I'm playing around with my own little memory manager, where I get the the Process Heap handle and within this I HeapAlloc and HeapFree as I manipulate data.

What I'm noticing in Process Explorer, is that my memory usage signature is not reducing went calling  HeapFree....

I'm just wondering if this is a general problem or does one have to use some other system cleanup functions ?



.Const

.Data?

.Data
;--- MEMORY ALLOC CONTROL STRUCTURE ---
MemStruct Struct
MemHandle DD 0 ;Pointer to handle pointer
MemMapPtr DD 0 ;Mem/Handle pointer
MemMapSize DD 0 ;Size of buffer
Dummy DD 0 ;extra to round off 16 bytes
MemStruct Ends

;--- MAIN MEMORY ALLOCATION UNIT ---
my_MemoryMap Struct
MainHeap DD 0 ;Module Local heap handle
MainHeapSize DD 0 ;Module Memory block size

Mem_01 MemStruct <> ;Individual Memory slots
Mem_02 MemStruct <>
Mem_03 MemStruct <>
Mem_04 MemStruct <>
Mem_05 MemStruct <>
Mem_06 MemStruct <>
Mem_07 MemStruct <>
Mem_08 MemStruct <>
Mem_09 MemStruct <>
Mem_10 MemStruct <>
Mem_11 MemStruct <>
Mem_12 MemStruct <>
Mem_13 MemStruct <>
Mem_14 MemStruct <>
Mem_15 MemStruct <>
Mem_16 MemStruct <>

MemFin DD 4 DUP (0FFFFFFFFh) ;EOL indicators
my_MemoryMap Ends

ErrorSTR DB 512 DUP(0)
.Code
;------------------------------------------------------------------------------
;INIT_MainHeap: Get MainHeap, and Base Pointers
;------------------------------------------------------------------------------
GET_MainHeap Proc hWnd:DWord

Invoke GetProcessHeap
Mov h_ProcessHeap, Eax
Return eax

GET_MainHeap EndP
;------------------------------------------------------------------------------
;Allocates memory from the main ProcessHeap for the calling module
;
;PARAMETERS: ptr_MemStuff -> MemStuff structure
;
;Requirements MemStuff.MainHeapSize must contain required memory size
;
;RETURNS: FALSE if :- 1)Windows HeapAlloc fails
; 2)MemStuff.MainHeapSize = 0
; MemStuff.MainHeap is also set to 0 on return
;
; TRUE if :- Windows HeapAlloc succeeds
; MemStuff.MainHeap contains local heap handle
; The rest of the MemStuff structure is zeroed
;------------------------------------------------------------------------------
FindLocalMemory Proc USES EDI ESI ptr_MemStuff:DWord

ASSUME ESI: PTR my_MemoryMap

mov esi, ptr_MemStuff
mov eax, [esi].MainHeapSize
cmp eax, 0
jne @F

FLM_Err_EX:
mov [esi].MainHeap, eax
Return FALSE

@@:

Invoke HeapAlloc, h_ProcessHeap, HEAP_ZERO_MEMORY, eax
cmp eax, 0
je FLM_Err_EX
mov [esi].MainHeap, eax

lea edi, [esi].Mem_01
cld
mov ecx, 4 * 16
xor eax, eax
rep stosd

mov eax, 0FFFFFFFFh
mov ecx, 4
rep stosd

Return TRUE

ASSUME ESI: NOTHING
FindLocalMemory Endp
;------------------------------------------------------------------------------
;Releases memory from the local Heap for the calling module
;
;PARAMETERS: ptr_MemStuff -> MemStuff structure
;
;Requirements MemStuff.MainHeap must contain local heap handle
;
;RETURNS: FALSE if :- 1)Windows HeapFree fails
; 2)MemStuff.MainHeap = 0
; MemStuff.MainHeap is NOT set to 0 on return
; The rest of the MemStuff structure is untouched
;
; TRUE if :- Windows HeapFree succeeds
; MemStuff.MainHeap is also set to 0 on return
; The rest of the MemStuff structure is zeroed
;------------------------------------------------------------------------------
LoseLocalMemory Proc ptr_MemStuff:DWord
ASSUME ESI: PTR my_MemoryMap

mov edi, ptr_MemStuff
mov eax, [esi].MainHeap
cmp eax, 0
jne @F

LLM_Err_EX:
invoke FillBuffer, ADDR ErrorSTR, 512, 0
invoke GetLastError
invoke dwtoa, eax, ADDR ErrorSTR
StrVal OFFSET ErrorSTR
Return FALSE

@@:
mov eax, [eax]
Invoke HeapFree, h_ProcessHeap, HEAP_NO_SERIALIZE, eax
cmp eax, 0
je LLM_Err_EX


mov edi, ptr_MemStuff
cld
mov ecx, 4 * 16 + 2
xor eax, eax
rep stosd

mov eax, 0FFFFFFFFh
mov ecx, 4
rep stosd

Return TRUE

ASSUME ESI: NOTHING
LoseLocalMemory Endp
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

nidud

#1
deleted

K_F

Thanks.. I'll look at that.

What I'm doing at the moment when the app starts is

1) Obtain a Process Heap Handle
2) As each MDI child window comes and goes, request/release a memory block (the code above) on the Process Heap Handle
3) In each MDI modules memory block I have a micro memory manager to (de)assign memory on the fly, within #2) memory block.

#2) is the problem where I cannot release the memory... which is essentially a Memory Leak, as the Process memory signature increases when I open the same MDI child window again. The problem being is that I'm not sure how much memory I'll need as the data info can be very large.
:(

edt: Come to think of if I've got a memory cleaner driver that does what I want... this must be using some memory cleaner function.. I'll see if I can find this.
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

K_F

Not to worry.. all fixed - just me getting stupid again :badgrin:

I changed the main allocation to HeapAlloc, and replaced ESI with EDI in two places - I missed this.. :dazzled:

Back to normal
:biggrin:
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'