free'ing memory

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
JJPeerless
Posts: 82
Joined: Mon Jun 20, 2005 3:32 am

free'ing memory

Post by JJPeerless »

hey, if i use loadImage to load up an png file into the lua image struct. How can i free ALL the memory used up by the image once im done with it. I need to be able to reuse all the memory.

Ive tried doing

free(imagename->data);
free(imagename);

but it doesnt work right..

any help..thanks
indianajonesilm
Posts: 15
Joined: Tue Feb 08, 2005 6:36 pm

Re: free'ing memory

Post by indianajonesilm »

JJPeerless wrote:hey, if i use loadImage to load up an png file into the lua image struct. How can i free ALL the memory used up by the image once im done with it. I need to be able to reuse all the memory.

Ive tried doing

free(imagename->data);
free(imagename);

but it doesnt work right..

any help..thanks
The lua manual says the following:
http://www.lua.org/manual/5.0/manual.html#2.9
Lua does automatic memory management. That means that you do not have to worry about allocating memory for new objects and freeing it when the objects are no longer needed. Lua manages memory automatically
I also remember Shine saying that he added Garbage Collection to Lua Player. Search the forums for "Garbage Collection".
JJPeerless
Posts: 82
Joined: Mon Jun 20, 2005 3:32 am

Post by JJPeerless »

well, im not using lua directly, I am just using his graphics library..but maybe I should look at some other stuff he has in there.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

Normally Lua does automatic memory management and this includes user objects, like Images, Music and Sound, too. But I've tested it and it looks like it runs the GC for some special Lua functions, only, like creating a table. The code below does some 170 iterations and says then it can't allocate another image. If you call the GC yourself with "collectgarbage()", it works forever. I've changed this in the new version (which I really hope to release this weekend :-) and the code below works forever without calling "collectgarbage()".

Code: Select all

white = Color.new(255, 255, 255)
black = Color.new(0, 0, 0)

c= 0
while true do
	image = Image.createEmpty(256, 256)
	screen:clear()
	screen:print(0, 0, c, white)
	c = c + 1
	screen.waitVblankStart()
	screen.flip()
	if Controls.read():start() then break end
end
chaos
Posts: 135
Joined: Sun Apr 10, 2005 5:05 pm

Post by chaos »

could you give us a list of what's going to change? i've currently got a couple thousand lines of code invested in a game i'm making, so i'm very interested in what's going to happen with the new version.
Chaosmachine Studios: High Quality Homebrew.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

chaos wrote:could you give us a list of what's going to change? i've currently got a couple thousand lines of code invested in a game i'm making, so i'm very interested in what's going to happen with the new version.
The next version will be backward compatible, except that alls numbers in Lua will be "float" instead of "double", so if your code doesn't work anymore, probably it will be a bug in Lua Player.
JJPeerless
Posts: 82
Joined: Mon Jun 20, 2005 3:32 am

Post by JJPeerless »

where can i find out exactly how the 'Garbage Collector" works..or how I can use it in my projects even tho im not using the lua player.. I currently only use the graphics.c / graphics.h file to handle png files. None of the other lua stuff...but when calling free(image->data) and free(image) it does not allow me to call malloc when needed on that free'd data..
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

JJPeerless wrote:where can i find out exactly how the 'Garbage Collector" works..or how I can use it in my projects even tho im not using the lua player..
The GC is integrated in the Lua library and I don't think that it is easy to use it without Lua. If you want to use GC with C or C++ programs, you have to use something like http://www.hpl.hp.com/personal/Hans_Boehm/gc/ or http://www.boost.org/ and you have to use special functions for allocating and using memory, which Lua does all for you, but for Lua scripts only :-)
JJPeerless wrote:but when calling free(image->data) and free(image) it does not allow me to call malloc when needed on that free'd data..
I don't understand you. If you are using C, how can you call something on something other at all? Do you mean you have some C++ class? Do you have some example code?
JJPeerless
Posts: 82
Joined: Mon Jun 20, 2005 3:32 am

Post by JJPeerless »

alright..for example

Image * picture1;

picture1 = loadImage("ms0:/picture1.png");

blah blah

then when im done with picture1 i call

free(picture1->data);
free(picture1);

..however this does not free the memory taken by picture1..

for example..if you did something like this:

int a;
for(a=0;a<200;a++)
{
Image * tempPic;
tempPic = loadImage("ms0:/temp.png");

free(tempPic->data);
free(tempPic);

}

you would run out of memory even tho I am trying to free all the memory used..

:(
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

JJPeerless wrote:int a;
for(a=0;a<200;a++)
{
Image * tempPic;
tempPic = loadImage("ms0:/temp.png");

free(tempPic->data);
free(tempPic);

}

you would run out of memory even tho I am trying to free all the memory used..

:(
This works for me. I've tried the code below with a 480x272 picture and no error occurs.

Code: Select all

int a; 
for&#40;a=0;a<200;a++&#41; &#123; 
	Image * tempPic; 
	tempPic = loadImage&#40;"background.png"&#41;; 
	if &#40;!tempPic&#41; &#123;
		printf&#40;"error"&#41;;
		break;
	&#125;
	free&#40;tempPic->data&#41;; 
	free&#40;tempPic&#41;; 
	printf&#40;"%i", a&#41;;

&#125; 
sceKernelSleepThread&#40;&#41;;
Post Reply