News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Whats wrong with GlobalFree API?

Started by Farabi, May 18, 2007, 12:46:22 AM

Previous topic - Next topic

Farabi

Im creating a word table but it seems something is wrong with the GlobalFree API. When I try to destroy the table a error occur and I dont know what is that. Am I wrong using the GlobalFree API?
Sorry for my lack of english.


DestroyWordTable proc uses esi edi lpsf:Dword
LOCAL w_cnt,l_cnt:dword

                ;The Structure are
; 0  header = txt1
; 4  lpmem file
; 8  lpmem line table
; 12 dword size
; 16 line count
; 20 for use with process function
; 24 for use with word function
; 28 word count

mov esi,lpsf
cmp esi,0
jnz @f
ret ; if the handle is 0 it invalid. Dont continue.
@@:
; check for word count on a line
; loop each lines
; release each word

mov ecx,[esi+16] ; line count
mov l_cnt,0
loop_each_lines:
push ecx
mov edx,[esi+28] ; esi+28 is word count array
mov eax,l_cnt ; eax is the line counter
mov ecx,[edx+eax*4] ; ecx is the number of word
cmp ecx,0 ; if it zero
jz no_word ; dont continue
mov w_cnt,0 ; zero the counter
loop_each_word: ;
push ecx ;
mov eax,w_cnt ;
mov edx,[esi+24] ;
invoke GlobalFree,[edx+eax*4] ; release the word information memory. it 20 byte each
pop ecx ;
inc w_cnt ;
dec ecx ;
jnz loop_each_word ;
no_word:
pop ecx
inc l_cnt
dec ecx
jnz loop_each_lines


ret
DestroyWordTable endp

Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

raymond

For each GlobalAlloc you would use, you are returned a handle for the amount of memory you reserved. That handle is also the address of the start of that memory block.
When you want to release that specific memory block, you provide its handle to the GlobalFree API which releases it entirely and your app cannot have any more access to it.

Your posted code does not show how you reserved the memory you are trying to release. I will not even attempt to make any assumption on how you did it.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

donkey

Hi Farabi,

Not that I can add anything to what Raymond said, after all he is perfectly correct that without knowing how the memory was allocated it is not possible to suggest where the problem might be, but looking at the code don't you find it a bit wasteful to allocate each structure individually ? After all it would seem that you would have quite a few structures and it would certainly be more efficient to use a large expandable memory block of the type VirtualAlloc gives you. You could for example, allocate memory in 4MB blocks expanding when necessary and free yourself of all of the GlobalFree problems by freeing the memory with a single api call. It would not be any more difficult to track, actually easier as you could find a record simply with MEMBASE + (RECNUM*SIZE), eliminating the need to store addresses in a parallel array. Also your application looks to me like a perfect place to use a linked list, another form that lends itself to the large memory allocations. Sorry for getting a little pedantic and a bit off-topic.

Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Farabi

Here is the code if you want to see how am I creating the table.


CreateWordTable proc uses esi edi lpsf:dword
LOCAL tmp[20]:dword
LOCAL w_cnt,lew_cnt,w_ptr,td,ta:dword
LOCAL td1,tc1,offst:dword
; This function will create a word table filled with offset and size

mov esi,lpsf
lea edi,tmp


mov edx,[esi+16]
shl edx,2
invoke GlobalAlloc,GMEM_ZEROINIT or GMEM_FIXED,edx
mov [esi+24],eax
mov dword ptr[eax],"NANO"

mov edx,[esi+16]
shl edx,2
invoke GlobalAlloc,GMEM_ZEROINIT or GMEM_FIXED,edx
mov [esi+28],eax
; mov dword ptr[eax],"NANO"


mov td1,1
mov ecx,[esi+16]
loop_each_lines:
push ecx
invoke TextFromFile,td1,lpsf
invoke Word_Count,edx,eax
mov w_cnt,eax
cmp eax,0
jle no_word
; For word count
pushad
mov edx,[esi+28]
mov ecx,td1
dec ecx
mov [edx+ecx*4],eax
popad
xchg eax,edx
shl edx,2
invoke GlobalAlloc,GMEM_ZEROINIT or GMEM_FIXED,edx
mov w_ptr,eax
mov edx,[esi+24]
mov ecx,td1
dec ecx
mov [edx+ecx*4],eax

mov tc1,1
loop_each_word:
push eax
invoke TextFromFile,td1,lpsf
invoke Word_position,tc1,edx,eax
push edx
push eax
invoke GlobalAlloc,GMEM_ZEROINIT or GMEM_FIXED,20
mov ecx,eax
pop eax
pop edx
mov dword ptr[ecx],"FFFF" ; Header
mov [ecx+4],edx ; offset
mov [ecx+8],eax ; size
mov eax,w_cnt
mov [ecx+12],eax ; word count
pop eax

mov edx,tc1
dec edx
mov [eax+edx*4],ecx

inc tc1
dec w_cnt
jnz loop_each_word
no_word:
pop ecx
inc td1
dec ecx
jnz loop_each_lines


ret
CreateWordTable endp

Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"