January 11th, 2013, 08:11 AM
Invalid access to memory location using GlobalFree
Under Microsoft, I use GlobalAlloc and GlobalFree to dynamically allocate and free memory. Intermittently GlobalFree is reporting error 998 (GetLastError() - Invalid access to memory location). I've checked my logs of addresses returned by GlobalAlloc against those being freed by GlobalFree and when the problem occurs the address causing the error has been allocated by GlobalAlloc and has not been freed before since allocation.
I can't reproduce it. Currently I'm checking for error 998 from GlobalFree and ignoring it if it occurs - which is not satisfactory. I have one function (AllocMem) that calls GlobalAlloc and checks for errors and another function (FreeMem) which calls GlobalFree and checks for errors. My program uses AllocMem and FreeMem everywhere memory is required or freed. AllocMem and FreeMem are in a DLL. The program is heavily threaded and a different thread may call AllocMem to the one that calls FreeMem. I don't believe I've got a race condition etc as all the code in the program containing the AllocMem and FreeMem calls are part of a CriticalSection using the same variable.
Could some kind guru explain what is a possible cause of this error?
Any help appreciated.
Click Here to Expand Forum to Full Width