gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > bug hunting!

#141183 - spinal_cord - Sun Sep 23, 2007 12:46 am

I'm currently trying to get my stupid menu to work, but whenever I re-read a directory I get gfx problems so I assume I have a leak somewhere. Can someone tell me if there are any problems with the following code,

Code:

void get_file_list(void)
{
// loading animation
// sprite for loading animation
PA_LoadSprite16cPal(1,2,(void*)loading_Pal);   // Palette name
PA_CreateSprite(1, 10,(void*)loading_Sprite,OBJ_SIZE_32X32,0,2,96,120);

int temp;

   number_of_files=0; // so we have no files
   struct stat st;
   DIR_ITER* dir;
   dir = diropen (folder);
   if (dir == NULL) {
      // can't open
   } else {
      while (dirnext(dir, icon[number_of_files].filename, &st) == 0) {
         icon[number_of_files].is_folder = (st.st_mode & S_IFDIR) ? 1 : 0;
         
if(st.st_mode & S_IFDIR)
{
 // is a folder
 // later load the folder icon and palette
}else{   
 if(isNDS(icon[number_of_files].filename))
 {
   if(number_of_files<max_files)
   {
      icon[number_of_files].is_folder=0; // is not folder
    readheader(icon[number_of_files].filename,number_of_files);


    number_of_files++;//next line
   // animate the loading anim
    PA_SetSpriteAnim(1, 10, lframe);
   lframe++;
   PA_WaitForVBL();
    if(lframe==12)lframe=0;
   }   
 }

}      
}
dirclose(dir);
PA_WaitForVBL();
}   
// delete the animation
PA_DeleteSprite(1,10);

number_of_files--; // remove the last unused entry

   for(temp=0; temp<=number_of_files; temp++)
   {
      icon[temp].x=0;
      icon[temp].y=temp*32;
      icon[temp].zoom=256;
      icon[temp].sprite=-1;   // Keep track of sprite used for each icon.
   }
   
}




I'm likely to spend forever on this, so I need all the help I can get.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage

#141394 - Miked0801 - Mon Sep 24, 2007 11:12 pm

Without being familiar with the API, here's my thoughts:

Code:

     // can't open
   } else {
      while (dirnext(dir, icon[number_of_files].filename, &st) == 0) {
         icon[number_of_files].is_folder = (st.st_mode & S_IFDIR) ? 1 : 0


As there is no limit to how often this will repeat, you could be writing off the end of the icon[] array.

Scratch that, you are never loading number_of_files with any value. It will always be 0. Perhaps you wanted a ++ on there?

Oh #$%@, your silly indentation screwed me up. Keeps reading...

Why '--' on number of files? Just use < instead of <= in the for()

Hmm, number_of_files is global. Any chance an interrupt is playing with it as all?

Hmm. Are you 100% sure you are handling your max_files condition correctly?

Also, your SetSpriteAnim call is using magically delicious numbers. Are you sure those number are valid?

lframe is never set to a low value to begin and appears to be global. If it starts above 12, it will never reset to 0. Consider using >= on your if.



That's what I got first pass. Others may be able to help more.

#141397 - spinal_cord - Tue Sep 25, 2007 12:37 am

Quote:
Why '--' on number of files? Just use < instead of <= in the for()

I'm number_of_files++ after adding the info to the array, so the last entry is never used. THis way I dont need to remember to number_of_files-1 in the icon rendering.

Quote:
Hmm, number_of_files is global. Any chance an interrupt is playing with it as all?


This is the only place n the entire code where number_of_files is used (I think)

Quote:
Hmm. Are you 100% sure you are handling your max_files condition correctly?


How do you mean? If number_of_files = max_files, then that last entry is overwritten. Might not be the best way, but it works (I think).

Quote:
Also, your SetSpriteAnim call is using magically delicious numbers. Are you sure those number are valid?

lframe is never set to a low value to begin and appears to be global. If it starts above 12, it will never reset to 0. Consider using >= on your if.

I still have the bug when I completely remove the sprite animation. lframe is global but I can't remember why.

At least you tried, thankyou.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage

#141406 - Miked0801 - Tue Sep 25, 2007 2:11 am

Perhaps your icon rendering code is getting confused when you reload its new information upon directory reload? Another guess.

#141410 - spinal_cord - Tue Sep 25, 2007 2:24 am

If your using palib, I can pm you a link to the full code if you want to have a look. its bugging the hell out of me (pun intentional).
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage

#141423 - Vich - Tue Sep 25, 2007 9:04 am

I don't know how diropen works, but maybe you do something wrong with the directory data pointers?

You do:
dir = diropen (folder);
But is "folder" a block of allocated memory and is it big enough for diropen?
_________________
[project website] [personal website]

#141456 - tepples - Tue Sep 25, 2007 8:14 pm

'folder' is supposed to be a C string containing the path of the folder to open. But because 'folder' is not a local variable, it has to be a variable declared directly in the module, either globally or "static". For example:
Code:
char folder[PATH_MAX];

Am I right?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#141504 - spinal_cord - Wed Sep 26, 2007 11:00 am

char * folder="/";
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage