News:

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

Problem with globalfree on Win7

Started by Farabi, July 26, 2010, 12:13:47 PM

Previous topic - Next topic

Farabi

Im making an simple accounting system for my lecture, my software always crashed each time I free the memory using GlobalFree, did anyone experienced the same thing?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

KeepingRealBusy


Farabi

Quote from: KeepingRealBusy on July 26, 2010, 02:48:09 PM
How did you allocate the memory?


invoke GlobalAlloc,GMEM_ZEROINIT or GMEM_FIXED,nSize
.if eax==0
invoke PERR
invoke MessageBox,NULL,addr mem_error,NULL,MB_OK
.endif


I dont know if it was just me or anyone else experienced the same thing.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

oex

You are allocating alright I think but are you sure you are freeing the right pointer address on end?

invoke GlobalFree, MemoryAddress

Also *some* windows functions manage their own functionality or have their own memory free functions you should check windows API notes to be on the safe side
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

hoverlees

I think you may pass a wrong handle to the GlobalFree function.

Farabi

Quote from: hoverlees on July 27, 2010, 01:41:38 AM
I think you may pass a wrong handle to the GlobalFree function.

Maybe, I used to allocate the memory and add it with 4.
for example

invoke malloc,128
add eax,4
invoke globalfree,eax


Is that now not allowed on Win7?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Slugsnack

No you should not change the pointer. The pointer effectively points to an object. By adding 4, you are now pointing to something else completely.

oex

Do this....

mov eax, alloc(128)
mov ebx, 100
mov [eax], ebx
mov [eax+4], ebx
etc
free eax

Note: eax has not changed.... if you try and free eax and you have changed it *your application will error
*Almost always

Also you could do this:

LOCAL Mem:DWORD

mov Mem, alloc(128) <---- Mem is Pointer to 128 byte buffer ie like LOCAL buff[128]:BYTE.... Mem = PTR buff (except created dynamically)
mov eax, Mem
mov ebx, 100
mov [eax], ebx
mov [eax+4], ebx
etc
free Mem
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

sinsi

Quote from: Farabi on July 27, 2010, 06:35:56 AMIs that now not allowed on Win7?
I'm sure it's never been allowed in any windows.

Quote from: SDKhMem A handle to the global memory object. This handle is returned by either the GlobalAlloc or GlobalReAlloc function.
You can't give it a bogus handle, but maybe win7 is stricter?
Light travels faster than sound, that's why some people seem bright until you hear them.

MichaelW

Passing the pointer returned by malloc to GlobalFree seems strange, but it appears to work OK on my system, and the same with GlobalReAlloc.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    invoke crt_malloc, 1000
    mov ebx, eax
    print hex$(eax),13,10

    invoke GlobalReAlloc, ebx, 1000, 0
    print hex$(eax),13,10               ; returns handle on success

    invoke GlobalFree, ebx
    print hex$(eax),13,10,13,10         ; returns null on success


    invoke crt_malloc, 1000
    mov ebx, eax
    print hex$(eax),13,10

    add ebx, 4
    invoke GlobalFree, ebx
    print hex$(eax),13,10               ; returns handle on fail

    print LastError$(),13,10,13,10

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


Note in the results that the ebx+4 value causes GlobalFree to return a different handle.

00792428
00792428
00000000

007939E0
007939E4
The handle is invalid.

eschew obfuscation