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 

Problem porting a game from PC to PSP

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



Joined: 03 Oct 2005
Posts: 63

PostPosted: Sun May 21, 2006 11:26 pm    Post subject: Problem porting a game from PC to PSP Reply with quote

Hi,

I've been trying to port one of my games to PSP lately, but I ran into a weird crash which does not really make any sense at all.

Code:
int FindClosestEdge(const Vec3& pos, float maxDist, Vec3& outPos)
{
   int      bestIdx = UNDEF_IDX;
   float   bestDistSqr = FLT_MAX;
   float   maxDistSqr = sqr(maxDist);
   Vec3   bestPos;
   Vec3   out;

   for(size_t i = 0; i < m_edges.size(); i++)
   {
      if(m_edges[i].type == 0)
         continue;
      float   distSqr = DistanceSqrToLine(pos, m_verts[m_edges[i].va], m_verts[m_edges[i].vb], out);
      if(distSqr < maxDistSqr && distSqr < bestDistSqr)
      {
         // IT CRASHES HERE...
         bestDistSqr = distSqr;
         bestIdx = (int)i;
         bestPos = out;
      }
   }
   outPos = bestPos;
   return bestIdx;
}


Basically the code crashes when it tries to store some data to a varialbe which is in stack. At least that is my hypothesis. All the data is fine, if I just loop through the list and print the numbers on screen everything is just fine. The call stack is no more than 4-5 functions deep.

My question is... how should I start debugging this thing? Is there some stuff which I can use to keep track of my stack & heap usage? Or any other educated guesses what might cause that even if the amount of information is very limited here?

And the crash... it just hangs and eventually psp shutsdown itself.


--mikko
Back to top
View user's profile Send private message
ooPo
Site Admin


Joined: 17 Jan 2004
Posts: 2032
Location: Canada

PostPosted: Mon May 22, 2006 5:19 am    Post subject: Reply with quote

Code:
if(distSqr < maxDistSqr && distSqr < bestDistSqr)

That looks very odd. Are you sure it ever worked? Try this:

Code:
if((distSqr < maxDistSqr) && (distSqr < bestDistSqr))
Back to top
View user's profile Send private message Visit poster's website
memon



Joined: 03 Oct 2005
Posts: 63

PostPosted: Mon May 22, 2006 7:03 am    Post subject: Reply with quote

Yep, it returns correct results on my pc version and comparison operators have higher precedense (executed first) than logic ops.
http://www.cppreference.com/operator_precedence.html
The place where the C operator precedense usually gets hairy when you need to mask and shift, since shift has higher precedence than bitwise and and or. Example:
Code:
int a = b & 0xff << 8;
vs.
int a = (b & 0xff) << 8;


Anyway... that should not cause the crash, though. Actually if I just comment out the contents of the if block it does not crash.

And any further use of the result from that function is not used at the moment. That is, the program returns back to main menu. And that works if I comment out the block of code that stores the result.
Back to top
View user's profile Send private message
dot_blank



Joined: 28 Sep 2005
Posts: 498
Location: Brasil

PostPosted: Mon May 22, 2006 8:56 am    Post subject: Reply with quote

why use this
Code:
for(size_t i = 0; i < m_edges.size(); i++)

when you should use this
Code:

size_t i;
for(i = 0; i < m_edges.size(); i++)

_________________
10011011 00101010 11010111 10001001 10111010
Back to top
View user's profile Send private message
ufoz



Joined: 10 Nov 2005
Posts: 86
Location: Tokyo

PostPosted: Mon May 22, 2006 10:07 am    Post subject: Reply with quote

dot_blank wrote:
why use this
Code:
for(size_t i = 0; i < m_edges.size(); i++)

when you should use this
Code:

size_t i;
for(i = 0; i < m_edges.size(); i++)


um, because the first is the cleaner and accepted way to do loops in c++ when you don't need the loop variable later on? that part is perfectly fine.

with weird crash bugs like these, for me, it's usually some other part of the code with messy pointer handling or a buffer overrun or somesuch, screwing up the memory enough to have it crash in unexpected locations, your code looks fine.
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger
dot_blank



Joined: 28 Sep 2005
Posts: 498
Location: Brasil

PostPosted: Mon May 22, 2006 12:28 pm    Post subject: Reply with quote

yes i understand its standard in C++ but code looked
like standard C ....my bad if that wasnt clear
_________________
10011011 00101010 11010111 10001001 10111010
Back to top
View user's profile Send private message
ooPo
Site Admin


Joined: 17 Jan 2004
Posts: 2032
Location: Canada

PostPosted: Mon May 22, 2006 2:09 pm    Post subject: Re: Problem porting a game from PC to PSP Reply with quote

memon wrote:
Vec3 bestPos;

(and later on...)

bestPos = out;

How is Vec3 defined? And under what compiler/os does this code work?
Back to top
View user's profile Send private message Visit poster's website
memon



Joined: 03 Oct 2005
Posts: 63

PostPosted: Mon May 22, 2006 7:31 pm    Post subject: Reply with quote

The code is indeed C++ (I think that const reference would have been a good clue ;)). The Vec3 is a class and that code works well under VC7 and GCC. The same vector class is used in code which I can verify working.

I suspected bad pointers at first too. I have douple checked a lot of code to make sure that does not happen (cannot be 100% sure of course). I use STL containers alot so there should not be that much the normal pointer mishaps. Bad pointers usually make the stuff to crash when trying to read or write beyond valid range, but this thing just hangs which makes me think that it might be something else.

Still, I would like to first verify the heap and stack usage. Any pointers to that stuff?
Back to top
View user's profile Send private message
ector



Joined: 12 May 2005
Posts: 195

PostPosted: Tue May 23, 2006 4:16 am    Post subject: Reply with quote

Maybe you're accidentally declaring some brutally huge variables on the stack in the function tree that is calling this function? Maybe you passed in a too small value for stack size when you created the thread that's running this?
_________________
http://www.dtek.chalmers.se/~tronic/PSPTexTool.zip Free texture converter for PSP with source. More to come.
Back to top
View user's profile Send private message
memon



Joined: 03 Oct 2005
Posts: 63

PostPosted: Tue May 23, 2006 6:08 am    Post subject: Reply with quote

ector, Where do I define the stack size? I tried to look around but could not really found anything. My entrypoint of the code is pretty much based on the cube sample. I dont create any extra threads in my program.

The another thing is how do I keep track of my stack? Is there some variable or function I can use to get the current stack size?
Back to top
View user's profile Send private message
dot_blank



Joined: 28 Sep 2005
Posts: 498
Location: Brasil

PostPosted: Tue May 23, 2006 10:02 am    Post subject: Reply with quote

Code:

int sceKernelGetThreadStackFreeSize ( SceUID thid );
//Get the free stack size for a thread.

/* Define the main thread's stack size (optional) */
/**
 * ELF:
 * Elf default stack size is (256K)
 *
 * PRX:
 * PRX default stack size is (256K)
 *   
 * Example Below: smaller stack for kernel
 *                thread for 1.0 psp (32K)
 */
PSP_MAIN_THREAD_STACK_SIZE_KB(32);


const does not tell me much as that can be used in C
hope maybe the above helps you a bit :P
_________________
10011011 00101010 11010111 10001001 10111010
Back to top
View user's profile Send private message
PlayfulPuppy



Joined: 05 May 2006
Posts: 22

PostPosted: Tue May 23, 2006 9:41 pm    Post subject: Reply with quote

Is the crash precisely where that comment is (Either during or just after the square root test block) or is it somewhere within that block? I'm assuming the latter at the moment.

Although I'm fairly sure I'm wrong about this, I have my eye on the int cast you're doing there. I've had troubles before when casting between types of different sizes (I'm not too sure how big a size_t is on the PSP, it may be 8 bytes or it might be 4), but in my experience that's only ever been when trying to cast pointers that aren't aligned properly, so it's a long shot at best.

Still, humor both me and you and try commenting out that line (And, one by one, the ones surrounding it) to see if you can get it to pass that section.

Finally, do you have any copy consructors or overloaded assignment operators for Vec3? If so, the problem might be occuring in there.
Back to top
View user's profile Send private message
memon



Joined: 03 Oct 2005
Posts: 63

PostPosted: Tue May 23, 2006 9:57 pm    Post subject: Reply with quote

Thanks for the replies. After a bit a twiddling yesterday I got another crash. This time it was floating point exception. I think it is time to install that psplink :)

I think I will still try to check that stack usage too. The code crashed if I had any assignment inside the if.
Back to top
View user's profile Send private message
CyberBill



Joined: 26 Jul 2005
Posts: 86
Location: Redmond, WA

PostPosted: Wed May 24, 2006 7:35 am    Post subject: Reply with quote

Ahh yes, the PSP will hang when it hits floating point exceptions, where as PC will just continue on and put a value like #INF or 0 in its place.

Ensure that you arnt doing any divide by 0s, or any overflows or underflows. To really stress your code, use something like this:

Code:

float intToFloat(unsigned i)
{
   return *(float*)&i;
}

for(int i=0; i<=0xFFFFFFFF; i++)
  for(int j=0; j<=0xFFFFFFFF; j++)
    for(int k=0; k<=0xFFFFFFFF; k++)
    {
       Vec3f bruteVector( intToFloat(i), intToFloat(j), intToFloat(k) );
       DistanceSqrToLine( bruteVector, m_verts... blah blah blah );
    }


Anyways, you get the idea. ;) On the PSP that'll pretty much always crash... you might want to put checks for NAN in intToFloat() to instead return 0 in those cases.... just a thought! :)

-Bill
Back to top
View user's profile Send private message AIM Address MSN Messenger
groepaz



Joined: 01 Sep 2005
Posts: 305

PostPosted: Wed May 24, 2006 8:19 am    Post subject: Reply with quote

you can disable the floatingpoint exceptions. (i dont recall how though, but it was answered in the forum at some point, searching should help :))
_________________
http://www.hitmen-console.org
http://hitmen.c02.at/files/yapspd/
Back to top
View user's profile Send private message Visit poster's website
CyberBill



Joined: 26 Jul 2005
Posts: 86
Location: Redmond, WA

PostPosted: Wed May 24, 2006 9:02 am    Post subject: Reply with quote

Dont disable exceptions....

Its more complicated and a bigger pain in the ass to worry about your game objects having #INF or #NAN values than it is to just fix the algorithms that make them creep in in the first place.
Back to top
View user's profile Send private message AIM Address MSN Messenger
memon



Joined: 03 Oct 2005
Posts: 63

PostPosted: Wed May 24, 2006 6:20 pm    Post subject: Reply with quote

I think I'll try to keep them enabled then. Any pointers to articles of good floating point practices? The new features in psplink sound promising too regarding my debugging needs... I think it's going to be interesting weekend then :)
Back to top
View user's profile Send private message
ector



Joined: 12 May 2005
Posts: 195

PostPosted: Wed May 24, 2006 9:47 pm    Post subject: Reply with quote

If you prefer to do your debugging on PC, you can just enable floating point exceptions on the PC instead (they're disabled by default) and do your debugging there...
_________________
http://www.dtek.chalmers.se/~tronic/PSPTexTool.zip Free texture converter for PSP with source. More to come.
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