Page 1 of 1

AllocateMemory: what changed?

Posted: Sat Jul 17, 2004 7:16 pm
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.

Posted: Sat Jul 17, 2004 7:26 pm
by GPI
Old Method:

Code: Select all

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

Code: Select all

*Adr=AllocateMemory(Length)
...
FreeMemory(*Adr)

Posted: Sat Jul 17, 2004 8:42 pm
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]

Posted: Sat Jul 17, 2004 9:12 pm
by GaryCXJk
Well, that GPI already took care of in the old version, but thanks anyway :D