AllocateMemory: what changed?

Just starting out? Need help? Post your questions and find answers here.
GaryCXJk
New User
New User
Posts: 2
Joined: Sat Jul 17, 2004 6:59 pm

AllocateMemory: what changed?

Post by GaryCXJk »

Well, first of all, I'm actually new to PureBasic ,though I made some cool additions to GPI's Charas.EX, original found on www.charas-project.net. Anyway, on the same project, I wanted to add some things to it, but the source didn't work on 3.90 of PB. Luckily somebody else fixed it (Chronic, on the Charas site). It compiles now, but there was some error that I wanted to fix.

If you downloaded the program (in the menu on top, click on Charas.EX) you can import your own resources you took from the Charas generators (currently only Charsets for Charas.EX). In the new source however there was a bug that when you did that the program would crash (actually, when you saved the imported resource). I've searched and added a lot of times the OpenWindow command, and I stumbled on the PokeB command in a for constructor. After long trying I found out that there probably wasn't enough memory to store, but I wondered how the AllocateMemory command worked.

mem=AllocateMemory(memory_import,#resource_uncompressed_size,0)

This was the original, that didn't work.

mem=AllocateMemory(memory_import)

This caused the crash.

mem=AllocateMemory(#resource_uncompressed_size)

This was the fix.

Well, I now am wondering, what does AllocateMemory do in the new version of PB? Does it now searches for the amount of free memory specified in the function? Because the help manual wasn't clear about that.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

Old Method:

Code: Select all

*adr=AllocateMemory(Number,Length)
...
FreeMemory(Number)
New Method:

Code: Select all

*Adr=AllocateMemory(Length)
...
FreeMemory(*Adr)
Dr_Pixel
User
User
Posts: 36
Joined: Fri Oct 24, 2003 1:36 pm

Post by Dr_Pixel »

And after the allocating, it is a good idea to check the pointer you specified, to be sure it is not 0

If the pointer is 0, the memory could not be allocated, and attempting to use it or free it will cause a crash.

So:

Code: Select all


*Adr=AllocateMemory(Length) 

If *Adr
   
  ;use the memory here

  FreeMemory(*Adr) 

Else

  ;memory allocation failed
  ;put up error message or something

EndIf
[/code]
Dr Pixel
GaryCXJk
New User
New User
Posts: 2
Joined: Sat Jul 17, 2004 6:59 pm

Post by GaryCXJk »

Well, that GPI already took care of in the old version, but thanks anyway :D
Post Reply