Page 1 of 1
Problems with LocalAlloc
Posted: Sun Dec 21, 2008 8:22 pm
by Controller
1) Tried to debug a program on Windows XP, which allocates 2 memorys and it most times returns the same address... (tested with 4.02, 4.20 and 4.30)... and after little time it crashes on the second LocalAlloc (Invalid memory access. (write error at address ...).
Strangest of all, when running a compiled version in debugger (olly debug), it works fine, or when running on Windows 98
2) On another program (currently discontinued), sometimes the program never returns from a LocalAlloc (Windows 98, not sure about PureBasic version)
Note: Typically i use LocalAlloc(0, size) (= LMEM_FIXED) only
Posted: Sun Dec 21, 2008 8:43 pm
by Fred
Posted: Sun Dec 21, 2008 9:27 pm
by Controller
thx, but seems the problem is quite simple and another reason for me to dislike Windows XP; Problem was I LocalFree-ed one memory twice, like this simple example:
Repeat
xLong1 = LocalAlloc_(0, 200)
xLong2 = LocalAlloc_(0, 200)
If xLong2 = xLong1
Debug "Doubled!"
EndIf
LocalFree_(xLong1)
LocalFree_(xLong1) ; (Should be xLong2)
ForEver
Posted: Mon Dec 22, 2008 1:16 am
by superadnim
why are you using the api, can't you use the pb functions? that'll make your application cross-platform. internally it'll use a heap in windows, which is proffered to globalalloc or localalloc anyway.
also, try handling your memory with a little of thought, always null your pointers and check if they aren't null before trying to free anything.
Posted: Mon Dec 22, 2008 11:42 am
by Controller
1) Cross-plattform is not needed (I primaly develop on Windows 98 and got enough probs with Windows XP incompatibility and dynamic unicode support). Esp. this project here uses API not integrated in PureBasic (Resources, waveIn) and some core parts are already written in assembly.
2) LocalAlloc may not be the best methode, but it's easy to use. I use it for dynamic memory allocation mostly.
3) I typically null pointers if it's needed, so this should not occur. Knowing this behaviour on Windows XP, I'll take even more care when writing the freeing stuff. As LocalFree() returns 0 (SUCCESS) both times on Windows XP, I'm glad I'm using Windows 98 because I can trap this errors here.
Posted: Mon Dec 22, 2008 3:44 pm
by superadnim
good luck supporting an unsupported OS. and maintaining such code

Posted: Mon Dec 22, 2008 7:01 pm
by dell_jockey
numerous compatibility issues exist within the several Windows versions - as you have discovered, so sticking to the PB command set where possible is a good thing, even if you never intend to roll out your app on a non-Windows OS...
Posted: Mon Dec 22, 2008 7:04 pm
by Fred
Using PB functions, it would have been spoted by the runtime debugger:
Code: Select all
Repeat
xLong1 = AllocateMemory(200)
xLong2 = AllocateMemory(200)
If xLong2 = xLong1
Debug "Doubled!"
EndIf
FreeMemory(xLong1)
FreeMemory(xLong1) ; (Should be xLong2)
ForEver
Posted: Mon Dec 22, 2008 9:21 pm
by Controller
Good reason indeed