free'ing memory
Moderators: Shine, Insert_witty_name
-
- Posts: 82
- Joined: Mon Jun 20, 2005 3:32 am
free'ing memory
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
Ive tried doing
free(imagename->data);
free(imagename);
but it doesnt work right..
any help..thanks
-
- Posts: 15
- Joined: Tue Feb 08, 2005 6:36 pm
Re: free'ing memory
The lua manual says the following: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
http://www.lua.org/manual/5.0/manual.html#2.9
I also remember Shine saying that he added Garbage Collection to Lua Player. Search the forums for "Garbage Collection".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
-
- Posts: 82
- Joined: Mon Jun 20, 2005 3:32 am
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
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.
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.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.
-
- Posts: 82
- Joined: Mon Jun 20, 2005 3:32 am
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..
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: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 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 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..
-
- Posts: 82
- Joined: Mon Jun 20, 2005 3:32 am
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..
:(
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..
:(
This works for me. I've tried the code below with a 480x272 picture and no error occurs.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..
:(
Code: Select all
int a;
for(a=0;a<200;a++) {
Image * tempPic;
tempPic = loadImage("background.png");
if (!tempPic) {
printf("error");
break;
}
free(tempPic->data);
free(tempPic);
printf("%i", a);
}
sceKernelSleepThread();