Strange behaviour of GlobalAlloc after first run from system bootup[masm32Win98]

Started by Light-I, April 05, 2006, 07:31:32 PM

Previous topic - Next topic

Light-I

I've got a problem, when I'm running my program first time after system loads, it not work poperly, but if I close it, next running comes w/o problems.
I've found some symptoms - this is my programm second thread :ProgrammThreadProc PROTO :DWORD
ProgrammThreadProc proc uses eax ebx ecx edx esi edi hWnd:DWORD
LOCAL PrgmWaitTime:DWORD
LOCAL TestWaitTime:DWORD
LOCAL ChipCommand:DWORD
LOCAL hMemPMemToBuff:HGLOBAL
LOCAL lpMemPMemToBuff:DWORD
LOCAL hMemBuffToPMem:HGLOBAL
LOCAL lpMemBuffToPMem:DWORD
LOCAL hMemFileToBuff:HGLOBAL
LOCAL lpMemFileToBuff:DWORD
LOCAL hMemTHexFile:HGLOBAL
LOCAL BinSize:DWORD
LOCAL MemAddr:DWORD
LOCAL SubBlock:DWORD
LOCAL lpMemPoint:DWORD
LOCAL lpLastMemPoint:DWORD
LOCAL lpDsplDst:DWORD
LOCAL lpDsplTick:DWORD
LOCAL ErrRet:DWORD
LOCAL CommErrCnt:DWORD
LOCAL Cnt100msTick:DWORD
LOCAL bEditTxtClr:BOOL
invoke GlobalAlloc, GHND, MaxMemSize ; GMEM_MOVEABLE or GMEM_ZEROINIT
mov hMemPMemToBuff, eax
mov lpMemPMemToBuff, NULL
invoke GlobalAlloc, GHND, MaxMemSize ; GMEM_MOVEABLE or GMEM_ZEROINIT
mov hMemBuffToPMem, eax
mov lpMemBuffToPMem, NULL
IFDEF _DEBUG_
invoke GlobalFlags, hMemPMemToBuff
PrintHex eax, "Global flags(hMemPMemToBuff)"
invoke GlobalFlags, hMemBuffToPMem
PrintHex eax, "Global flags(hMemBuffToPMem)"
PrintHex GMEM_DDESHARE
PrintHex GMEM_DISCARDABLE
PrintHex GMEM_DISCARDED
ENDIF
This piece of code produces this in VKDEBUG :
eax = 00004000, Global flags(hMemPMemToBuff) (ProcessData.asm, 34)
eax = 00004000, Global flags(hMemBuffToPMem) (ProcessData.asm, 36)
GMEM_DDESHARE = 00002000 (ProcessData.asm, 37)
GMEM_DISCARDABLE = 00000100 (ProcessData.asm, 38)
GMEM_DISCARDED = 00004000 (ProcessData.asm, 39)
System memory is 512MB (working & tested out of OS ). What's going - why allocated memory is discarded ? How can I fix it to able work every run ?
Thread is created in short procedure called from std WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM in WM_CREATE msg.
CreateThreadsObj PROTO :HWND
CreateThreadsObj proc hWnd:HWND
mov  bThreadQuit, FALSE
invoke CreateEvent, NULL, FALSE, FALSE, NULL
mov  hEventProgramm, eax
invoke CreateThread, NULL, NULL, ADDR ProgrammThreadProc, hWnd, NULL, ADDR ThreadIDProgramm
mov  hThreadProgramm, eax
ret
CreateThreadsObj endp

Light-I

I've got solution - MaxMemSize was initialised after calling procedure creating Thread ;) invoke CreateThreadsObj, hWnd
mov MaxMemSize, 8192 ; 8k
So if sbd meet similar problem, should test GlobalAlloc arguments first - allocating zero size memory global means creating discarded, but valid handle, which could be ReAllocate later...