Error loading 11th file (SDL_image)

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
r0b3rt
Posts: 17
Joined: Mon May 26, 2008 6:43 am

Error loading 11th file (SDL_image)

Post by r0b3rt »

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: Select all

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: Select all

	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);
	}
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Are you using PSP_HEAP_SIZE_KB(20480); or similar in your code to give yourself some heap to allocate from?
r0b3rt
Posts: 17
Joined: Mon May 26, 2008 6:43 am

Post by r0b3rt »

Yes, i am using PSP_HEAP_SIZE_KB(10240);
r0b3rt
Posts: 17
Joined: Mon May 26, 2008 6:43 am

Post by r0b3rt »

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

Code: Select all

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.
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Try setting the heap to 20480 and post back with your results.
r0b3rt
Posts: 17
Joined: Mon May 26, 2008 6:43 am

Post by r0b3rt »

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/7 ... p-rar.html

And thanks for helping me solve this mystery.
crazyc
Posts: 408
Joined: Fri Jun 17, 2005 10:13 am

Post by crazyc »

I bet the files arn't being closed after you are done with them.
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Nice catch crazyc.

Add:

Code: Select all

rwop->close(rwop);
r0b3rt
Posts: 17
Joined: Mon May 26, 2008 6:43 am

Post by r0b3rt »

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.
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

The limit is on the PSP, which is 10 open files.
Post Reply