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 

noob help - code crashes when exiting function

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



Joined: 04 Jan 2006
Posts: 4

PostPosted: Thu Jan 05, 2006 1:09 pm    Post subject: noob help - code crashes when exiting function Reply with quote

I have some code that seems to crash when the function returns. I lack the knowledge to debug it. Can some one suggest why my code dies once the function dirList() returns?

Code:
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspumd.h>
#include <pspiofilemgr.h>
#include <string.h>
#include <stdio.h>

PSP_MODULE_INFO("Get Dir", 0, 1, 1);

#define printf pspDebugScreenPrintf


/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
   sceKernelExitGame();

   return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
   int cbid;

   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);

   sceKernelSleepThreadCB();

   return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
   int thid = 0;

   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }

   return thid;
}

typedef struct listDir{
   int entCount;
   char dir[100][1000];
}listDir;


int dirList(char *root, listDir *directorylist)
{
   int dfd, count=0;
   dfd = sceIoDopen(root);
   SceIoDirent dir;
   printf("debug: Entered dirList()\n");
   while(sceIoDread(dfd, &dir) > 0)
   {
      if(dir.d_stat.st_attr == FIO_SO_IFDIR)
      {
         if( dir.d_name[0] != '.')
         {
            strcpy(directorylist->dir[count],dir.d_name);
            printf("debug: dir=%s\ndebug dir in struct=%s\n",dir.d_name,directorylist->dir[count]);
         } else {
         //printf("file: %s\n",dir.d_name);
         }
      }
      count++;
      directorylist->entCount=count;
   }
   printf("debug: struct entity count is %i\n",directorylist->entCount);
   sceIoDclose (dfd);
   printf("debug: Leaving dirList()\n");
   return 0;
}

/*int listFile(char *file)
{
   SceIoStat fileStat;
   sceIoGetstat(file, &fileStat);
   printf("File: %lli",fileStat.st_size);
}*/

int main()
{
   char dir[100];
   int count=0;
   listDir mylist;
   pspDebugScreenInit();
   SetupCallbacks();
   printf("Version 0.16\n");
   sprintf(dir,"ms0:/");
   mylist.entCount=0;
   int whocares=dirList(dir,&mylist);
   printf("We seem to crash here.\nTurning psp off.....but I am not sure why.\n");
   sleep(3);
   printf("Found %d objects\n",mylist.entCount);
   printf("debug: struct entity count is %i\n",mylist.entCount);
   while(mylist.entCount > count)
   {
      printf("Dir: %s\n", mylist.dir[count]);
      count++;
   }

   
   return 0;
}
Back to top
View user's profile Send private message MSN Messenger
Fanjita



Joined: 28 Sep 2005
Posts: 217

PostPosted: Fri Jan 06, 2006 1:23 am    Post subject: Reply with quote

If I remember rightly (I'm posting from work without much access to reference material) you need to initialise the "size" member of the dirinfo structure. It doesn't do any harm to memset the whole thing to zero, either.

I seem to recall people posting previous problems with the sceIoDread functions when using a dirinfo structure on the stack - you might have more joy using it off the stack - but I suspect that those are all related to not initialising ".size".

This same principle, of initialising the "size" member, applies to many other functions in the API, too.

EDIT: Forgot to mention why I think this causes the crash. If you haven't set up the size of the on-stack structure, then you're wide open to stack overflow which could corrupt the on-stack return address.
_________________
Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you!
Back to top
View user's profile Send private message
ronniebeck



Joined: 04 Jan 2006
Posts: 4

PostPosted: Sat Jan 07, 2006 8:20 pm    Post subject: Reply with quote

Hmmm can you suggest where I might learn how to do that?
Back to top
View user's profile Send private message MSN Messenger
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sat Jan 07, 2006 9:03 pm    Post subject: Reply with quote

Fanjita, unfortunately there isn't actually any size member in sceIoDirEntry, well there is but it is the size of the file ;) It just seems to be a bug in the MS driver as it works fine on all other devices such as flash and umd. ronniebeck I would just suggest setting the stack SceIoDirent variable to static and it at least shouldn't crash. Of course whether this is storing up trouble for later I wouldn't like to speculate :)
Back to top
View user's profile Send private message
jonny



Joined: 22 Sep 2005
Posts: 351

PostPosted: Sat Jan 07, 2006 9:36 pm    Post subject: Reply with quote

experienced the same problem with sceIoDread

the solution i've found is place a static global sceIoDirEntry variable and call sceIoDread always and only with this variable (all *seems* stable now :)

otherwise it badly crash, not only with sceIoDirEntry on the stack but even if you malloc an array of sceIoDirEntry in the heap
Back to top
View user's profile Send private message Visit poster's website
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Sat Jan 07, 2006 9:43 pm    Post subject: Reply with quote

Works fine for me on the stack or malloced as long as I memset the structure to 0 first, ie.
Code:

SceIoDirent dir;
memset(&dir, 0, sizeof dir);
ok = sceIoDread(dfd, &dir);


Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Sun Jan 08, 2006 6:13 am    Post subject: Reply with quote

In general, I'd recommend using newlib functions when possible, rather than calling Sce* directly, because in many cases we've already identified and worked around these sorts of quirks. In this case, opendir/readdir was fixed here.
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