Problems with LocalAlloc

Just starting out? Need help? Post your questions and find answers here.
User avatar
Controller
User
User
Posts: 28
Joined: Thu Jul 22, 2004 5:26 am
Location: Germany
Contact:

Problems with LocalAlloc

Post 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
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

User avatar
Controller
User
User
Posts: 28
Joined: Thu Jul 22, 2004 5:26 am
Location: Germany
Contact:

Post 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
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post 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.

:lol: should I bash the keyboard and give up?
:?
User avatar
Controller
User
User
Posts: 28
Joined: Thu Jul 22, 2004 5:26 am
Location: Germany
Contact:

Post 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.
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

good luck supporting an unsupported OS. and maintaining such code
:?

:lol: should I bash the keyboard and give up?
:?
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post 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...
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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
User avatar
Controller
User
User
Posts: 28
Joined: Thu Jul 22, 2004 5:26 am
Location: Germany
Contact:

Post by Controller »

Good reason indeed
Post Reply