using included function crashes DS

slenkar
Posts: 43
Joined: Sun Apr 17, 2011 8:10 pm

using included function crashes DS

Post by slenkar » Mon Sep 17, 2012 5:19 pm

here is a small example to crash the DS:

includeme.h

Code: Select all

#ifndef anims
#define anims
int new_int();
#endif

includeme.cpp

Code: Select all

int new_int()
{
int a;
return a;
}
main.cpp

Code: Select all

#include <nds.h>
#include <stdio.h>
#include <string.h>

#include "includeme.h"


int main() 
{	
	
int b=new_int();
while( 1 ) 
{
swiWaitForVBlank();
}
return 0;
} 


mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: using included function crashes DS

Post by mtheall » Mon Sep 17, 2012 9:57 pm

This works as expected for me. Maybe you can describe what is a "crash"?

slenkar
Posts: 43
Joined: Sun Apr 17, 2011 8:10 pm

Re: using included function crashes DS

Post by slenkar » Tue Sep 18, 2012 12:00 am

I compiled with GCC , are you using microsofts compiler?

When I run the nds file in the emulator it crashes.It just says 'Rom image has crashed'

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: using included function crashes DS

Post by elhobbs » Tue Sep 18, 2012 12:41 am

Emulators are not 100% accurate. What are you trying to prove with this example? Crashing an emulator is not particularly useful/meaningful in itself.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: using included function crashes DS

Post by mtheall » Tue Sep 18, 2012 2:39 am

Yes. Try a different emulator. Desmume is particularly good for testing homebrew

slenkar
Posts: 43
Joined: Sun Apr 17, 2011 8:10 pm

Re: using included function crashes DS

Post by slenkar » Tue Sep 18, 2012 2:55 pm

It also doesnt work on the hardware,or desmume

try adding a printf statement in the code and youll see that nothing is printed

then take out the call to the external function and it will work

slenkar
Posts: 43
Joined: Sun Apr 17, 2011 8:10 pm

Re: using included function crashes DS

Post by slenkar » Tue Sep 18, 2012 3:23 pm

oops I was running the wrong nds file!!

But I managed to recreate the crash succesfully now:

here is structs.h

Code: Select all

#ifndef structs
#define structs

#define one_off 1

#define torso_img 0
#define right_arm_img 1
#define right_forearm_img 2
#define left_arm_img 3
#define left_forearm_img 4

static inline void gxTranslate3f32( int32 x, int32 y, int32 z ) 
{
	MATRIX_TRANSLATE = x;
	MATRIX_TRANSLATE = y;
	MATRIX_TRANSLATE = z;
}
struct image
{
int width;
int height;
int handlex;
int handley;
int img_num;
};

//torso

struct body_part
{
char* name;
int img_num;
int x_pos;
int y_pos;
int angle;
m4x4 matrix;
};
struct limb_position
{
char *bodypart;
int angle;
int x_pos;
int y_pos;
int xscale; 
int yscale;
};

struct pose
{
limb_position limb_positions[30];
int num;
int pose_delay;
};	
struct animation 
{
 char *name;
 int anim_type;
 int pose_delay;
 int pose_timer;
 pose poses[30];
};

//#define new_string(my_string)  (char *)malloc(sizeof(char) * (strlen(my_string)+1));

char* new_string(const char *my_string);

struct character
{
body_part body_part_array[10];
int x_pos;
int y_pos;
};

void draw_character(character c);

#endif
here is main.cpp

Code: Select all

#include <nds.h>
#include <stdio.h>
#include "structs.h"
#include "heroanims.h"

//---------------------------------------------------------------------------------
int main(void) {
animation a =new_anim();
//---------------------------------------------------------------------------------
	consoleDemoInit();
	iprintf("this is a ds");
	while(1) {
		swiWaitForVBlank();
	}

}
heroanims.h

Code: Select all

ifndef anims
#define anims
animation new_anim();
#endif

if you move new_anim in main.cpp to outside of main() it works,
it crashes when new_anim is inside main()

also if I remove a piece of the animation struct it works

Code: Select all

poses pose[30];

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: using included function crashes DS

Post by elhobbs » Tue Sep 18, 2012 3:35 pm

the stack is 16k on the ds - you have overflowed the stack. your local declarations are too big - you will need to either make the global or dynamically allocate them (malloc or new). both will put the objects in main memory instead of the stack.

as a note - I choose to ignore
if you move new_anim in main.cpp to outside of main() it works,
it crashes when new_anim is inside main()
I have no idea what this means

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: using included function crashes DS

Post by mtheall » Tue Sep 18, 2012 4:46 pm

Yes definitely the you have overflowed the stack. The sizeof(animation) is 21856 bytes (quite a bit higher than the 16KB limit). Change "animation a = new_anim();" to "animation *a = new animation();" or "animation *a = (animation*)malloc(sizeof(animation));"

It appears that you have no idea what you are doing for allocations. Nobody in their right mind makes a function to allocate an object on the stack. It already happens automatically just by declaring the variable. "int b = new_int();" does not allocate a new 'b'; it is already allocated just from the "int b". All you are effectively doing is assigning to 'b' the value of 'a' from the new_int() function. Which is undefined because 'a' is uninitialized.

If you are doing exactly the same thing with new_anim(), then you are super overflowing the stack. "animation a" allocates the 21856 bytes, then in new_anim(), it allocates ANOTHER 21856 bytes;

slenkar
Posts: 43
Joined: Sun Apr 17, 2011 8:10 pm

Re: using included function crashes DS

Post by slenkar » Tue Sep 18, 2012 5:14 pm

oh i seee,

thanks I will be allocating everything from now on, (except maybe local ints and things like that)

Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests