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.

DS development > DS crash causing me to go insane

#108642 - FireSlash - Fri Nov 10, 2006 8:53 pm

So, heres what I know:
testObject isn't null
this inside testObject isn't null.
But a in spriteController::attachActor is, for some reason, null.

.....*twitch*
Code:
// === spriteController.h
// Sprite controller
// manages the OAM
// For great justice
 
#ifndef __class_spriteController
#define __class_spriteController
 
#include "managedObject.h"
#include "actor.h"
#include <nds.h>
 
class spriteController: public managedObject
{
   public:
      bool create(objectManager *instigator);
      void tick(float deltatime);
      bool destroy();
      // spriteController specific functions
      void moveSprite(int id, u16 x, u16 y);
      void rotateSprite(int id, u16 angle);
      int attachActor(actor *a);
      void detachActor(actor *a);
   protected:
      SpriteEntry *spriteMain;
      SpriteRotation *spriteRotationMain;
      int nextID;
};
#endif
 
 
// ===== spriteController.cpp
 
#include "spriteController.h"
 
int spriteController::attachActor(actor *a)
{
   //if (a == NULL)
   //   return 0;
   int id = nextID++; // HACK
   Coordinate position;
   position = a->getLocation(); // CRASHES HERE <<<<------ ;(
 
   spriteMain[0].attribute[0] =   ATTR0_COLOR_256 |
                           ATTR0_ROTSCALE_DOUBLE |
                           (int)position.y;
 
   spriteMain[0].attribute[1] =   ATTR1_ROTDATA(0) |
                           ATTR1_SIZE_64 |
                           (int)position.x;
 
   spriteMain[0].attribute[2] = id;
 
   dmaCopy(a->getGfxLoader()->image()->palette, (u16*)SPRITE_PALETTE, sizeof(a->getGfxLoader()->image()->palette));
   dmaCopy(a->getGfxLoader()->image()->data8, &SPRITE_GFX[id * 16], (a->getGfxLoader()->image()->width * a->getGfxLoader()->image()->height));
 
   return id;
}
 
// === testObject.h
// Object to debug with.
 
#include "actor.h"
#include "objectManager.h"
#include "spriteController.h"
 
class testObject: public actor
{
   public:
      bool create(objectManager *instigator);
      void tick(float deltatime);
   protected:
      int counter;
};
 
// === testObject.cpp
// Object to debug with
#include <nds.h>
#include <stdio.h>
 
#include "testObject.h"
 
bool testObject::create(objectManager *instigator)
{
   o = instigator;
   createGfx("mat.pcx");
   o->s->attachActor(this);
   return true;
}
 
void testObject::tick(float deltaTime)
{
   // Do stuff
}
//===== main.cpp
// Includes
 
#include <nds.h>
#include <fat.h>
 
#include "objectManager.h"
#include "testObject.h"
#include "videoController.h"
#include "spriteController.h"
 
int main(int argc, char ** argv)
{
   powerON(POWER_ALL_2D);
 
   irqInit();
   irqSet(IRQ_VBLANK, 0);
 
   objectManager manager;
   manager.create(); // Initialize!
 
   // Init fat routines
   fatInitDefault();
 
   // Create video controller
   videoController *v;
   v = new videoController;
   v->create(&manager); // Sets up vram banks
   manager.attach(v);
 
   manager.v = v;
 
   // Create sprite controller
   spriteController *s;
   s = new spriteController;
   s->create(&manager);
   manager.attach(s);
 
 
   // TEST
   testObject *test;
   test = new testObject;
   test->create(&manager);
 
   manager.attach(test);
 
   while (1)
   {
      manager.manage();
      //swiWaitForVBlank();
   }
 
   return 0;
}

_________________
FireSlash.net

#108647 - KoshNi - Fri Nov 10, 2006 9:31 pm

I don't like how this smells :

Code:
 o->s->attachActor(this); 


Does it mean :

Code:
 (o->s)->attachActor(this); 


or :

Code:
 o->(s->attachActor(this)); 


I don't know why, but it reminds me this song from Cypress Hill :

Quote:
When tha shit goes down ya better be
ready (when tha shit goes down)



Good luck guy...
_________________
Sunshine. Beauty. Love. Happiness.

#108649 - josath - Fri Nov 10, 2006 9:55 pm

and are you 100% sure that a is null? I'd put an if(a==null) iprintf("omg A is null!"); just to be sure. otherwise, there may be something else which is crashing but you don't realize.

If you can't get it to work, simplify it down to the simplest possible case you can:

Code:
clas blah {
  void attachFoo(foo *f) { if(f == null) iprintf("fails!"); else iprintf("ok!"); };
};

class foo {
  void testme(blah *b) { b->attachFoo(this); };
};

main() {
  blah *b = new blah();
  foo *f = new foo();
  foo->testme(b);
}


Then work your way up from there. I know it seems like a lot of work, but when there is no other option, you have to do things the brute-force way.

#108670 - pollier - Sat Nov 11, 2006 3:52 am

KoshNi wrote:
Does it mean :

Code:
 (o->s)->attachActor(this); 


or :

Code:
 o->(s->attachActor(this)); 


It's definitely the former; I don't think o->(s->attachActor(this)); is even valid C!
_________________
(Works for me!)

#108681 - FireSlash - Sat Nov 11, 2006 5:30 am

josath wrote:
and are you 100% sure that a is null? I'd put an if(a==null) iprintf("omg A is null!"); just to be sure. otherwise, there may be something else which is crashing but you don't realize.

If you can't get it to work, simplify it down to the simplest possible case you can:


I already tried this, hence how I discovered a was null..
If you uncomment the if... statement, the DS doesn't crash.

If you comment out the line referencing "a", it does not crash.

If you comment out everything else in the function but the a->getLocation() call, it crashes.
_________________
FireSlash.net

#108694 - simonjhall - Sat Nov 11, 2006 10:23 am

Have you tried building the code on the PC and then stepping through it on that? Yeah I know you can't catch DS-specific bugs, but it'd at least show you if something obvious is wrong.

OR, you could run it in one of the emulators and step through clock-by-clock.

Now that I think about it - if you send the code to me I'll try it with the debugger...but I'm about to go away for the weekend! I could look at it Sunday night if you like!

#108697 - OOPMan - Sat Nov 11, 2006 10:54 am

This definitely looks like a null pointer problem of some kind. Well, I think we're pretty certain of that already...

What you need to work out is why you're being passed a null pointer...

I wonder if you should/shouldn't be using the pointer deref (*) operators and/or reference (&) operators somewhere some that you are/aren't using them...

In other words, make sure your pointer operation is 100% kosher. The problem code uses pointers and it's often minor errors in such code that causes these kind of crashes...
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#108811 - FireSlash - Sun Nov 12, 2006 5:01 am

Found my null pointer.

I was cleaning up the names a bit, and I went back to look at the spriteController definition, only to find I never assigned objectManager a pointer to the spriteController.....

Hence, o->s <-- NULL.
_________________
FireSlash.net

#108842 - OOPMan - Sun Nov 12, 2006 8:18 am

Yes, Null pointers are one reason why use of references is encouraged now :-) Even though they don't solve all the problems...
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#108880 - FireSlash - Sun Nov 12, 2006 5:01 pm

I read up on references once, but they just looked like funny looking pointers to me.

Anyway, old habits die hard :/
_________________
FireSlash.net