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 

Error loading 11th file (SDL_image)

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



Joined: 26 May 2008
Posts: 17

PostPosted: Mon Jun 16, 2008 9:32 pm    Post subject: Error loading 11th file (SDL_image) Reply with quote

Hi, I have a strange problem. I am using this function to load and display image files using SDL, and everything works fine for the first 10 files.
When I try to display the 11th image, I get a: "Couldn't open name_of_the_image.bmp". And it doesn't matter if I'm displaying small or full screen images. I'm also monitoring amount of free memory and I think there's no problem there.

Code:

int displayImageUsingSDL(SDL_Surface *screen, const char* imageName, int posX, int posY){
    SDL_Surface *image = NULL;
    SDL_RWops *rwop = NULL;

    rwop=SDL_RWFromFile(imageName, "rb");
   image=IMG_LoadBMP_RW(rwop);

    if(!image){
      pspDebugScreenInit();
      pspDebugScreenPrintf("ERROR: %s\n", IMG_GetError());
      return -1;
   }
   
   //else drawtile(image, 0, 0, image->w, image->h, posX, posY, 0, 0, screen);
   //sceDisplayWaitVblankStart();
   //SDL_Flip(screen);
   
   SDL_FreeRW(rwop);
   SDL_FreeSurface(image);

   return 0;
}


So any idea where is the problem?

In the main program I have this:
Code:

   while(1){
      sceCtrlReadBufferPositive(&pad, 1);
      
      if(pad.Buttons & PSP_CTRL_CIRCLE) break;
      else if(pad.Buttons & PSP_CTRL_CROSS) displayImageUsingSDL(screen, "fullscreen.bmp", 0, 0);
      else if(pad.Buttons & PSP_CTRL_TRIANGLE) displayImageUsingSDL(screen, "small.bmp", 0, 0);
      else if(pad.Buttons & PSP_CTRL_SQUARE) printMemory();
      SDL_Delay(100);
   }
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Mon Jun 16, 2008 9:45 pm    Post subject: Reply with quote

Are you using PSP_HEAP_SIZE_KB(20480); or similar in your code to give yourself some heap to allocate from?
Back to top
View user's profile Send private message
r0b3rt



Joined: 26 May 2008
Posts: 17

PostPosted: Mon Jun 16, 2008 10:28 pm    Post subject: Reply with quote

Yes, i am using PSP_HEAP_SIZE_KB(10240);
Back to top
View user's profile Send private message
r0b3rt



Joined: 26 May 2008
Posts: 17

PostPosted: Mon Jun 16, 2008 10:35 pm    Post subject: Reply with quote

I am using these 3 ways to determine the amount of free memory (found 2 here, coded the third one myself):

Code:

void printMemory(){
   SceSize am = sceKernelTotalFreeMemSize();
   struct mallinfo mi;
   int mem, *myPtr = NULL, *myOldPtr = NULL, size = 64;

   mi = mallinfo();
   mem = (10240*1024) - mi.arena + mi.fordblks;

   pspDebugScreenPrintf("Available memory (heap size): %dbytes (%dKB or %dMB)\n", am, am/1024, am/1024/1024 );
   pspDebugScreenPrintf("Available memory: %dbytes (%dKB or %dMB)\n", mem, mem/1024, mem/1024/1024 );

   while(1){
      myPtr = (int*)realloc(myPtr, size);
      if (!myPtr){
         free(myOldPtr);
         size -= 64;
         break;
      }
      else{
         myOldPtr = myPtr;
         size += 64;
      }
   }
   pspDebugScreenPrintf("Available memory (by me): %dbytes (%dKB or %dMB)\n", size, size/1024, size/1024/1024 );
}


They are telling me I have 14/10/5 MB of memory free.
The values change only slightly after loading 10 images.
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Mon Jun 16, 2008 10:43 pm    Post subject: Reply with quote

Try setting the heap to 20480 and post back with your results.
Back to top
View user's profile Send private message
r0b3rt



Joined: 26 May 2008
Posts: 17

PostPosted: Mon Jun 16, 2008 11:02 pm    Post subject: Reply with quote

Tried it and still error @ 11th file.
If it will help, you can download the entire source code and EBOOT.PBP from:
http://www.megafileupload.com/en/file/70096/sdlbmp-rar.html

And thanks for helping me solve this mystery.
Back to top
View user's profile Send private message
crazyc



Joined: 17 Jun 2005
Posts: 410

PostPosted: Mon Jun 16, 2008 11:55 pm    Post subject: Reply with quote

I bet the files arn't being closed after you are done with them.
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Tue Jun 17, 2008 1:13 am    Post subject: Reply with quote

Nice catch crazyc.

Add:

Code:
rwop->close(rwop);
Back to top
View user's profile Send private message
r0b3rt



Joined: 26 May 2008
Posts: 17

PostPosted: Tue Jun 17, 2008 5:55 pm    Post subject: Reply with quote

Thanks for the help, works fine now.
Strange it crashed at the 11th file - I guess there's a limit on a number of opened SDL_RWops.
Thanks again.
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Tue Jun 17, 2008 7:36 pm    Post subject: Reply with quote

The limit is on the PSP, which is 10 open files.
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 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