forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

free'ing memory

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
JJPeerless



Joined: 20 Jun 2005
Posts: 82

PostPosted: Fri Sep 09, 2005 10:22 am    Post subject: free'ing memory Reply with quote

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
Back to top
View user's profile Send private message
indianajonesilm



Joined: 08 Feb 2005
Posts: 15

PostPosted: Fri Sep 09, 2005 12:45 pm    Post subject: Re: free'ing memory Reply with quote

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
Quote:
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".
Back to top
View user's profile Send private message
JJPeerless



Joined: 20 Jun 2005
Posts: 82

PostPosted: Fri Sep 09, 2005 2:41 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Fri Sep 09, 2005 6:27 pm    Post subject: Reply with quote

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:

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
Back to top
View user's profile Send private message
chaos



Joined: 10 Apr 2005
Posts: 135

PostPosted: Fri Sep 09, 2005 6:32 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Fri Sep 09, 2005 6:37 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
JJPeerless



Joined: 20 Jun 2005
Posts: 82

PostPosted: Sat Sep 10, 2005 12:58 am    Post subject: Reply with quote

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..
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sat Sep 10, 2005 2:34 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
JJPeerless



Joined: 20 Jun 2005
Posts: 82

PostPosted: Sat Sep 10, 2005 2:55 am    Post subject: Reply with quote

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

:(
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sat Sep 10, 2005 3:16 am    Post subject: Reply with quote

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:

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();
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group