Page 1 of 2

C++ -- class bah(0) works, but...[FIXED]

Posted: Sat Jun 09, 2007 5:21 pm
by earlz
Ok I have made a class, now when I try to just make this class as a normal variable(no indirection) it works good, but when I try to create a new class with new, it crashes somewhere inside of new(it never gets to the constructor code!)

I fail to see what I am doing wrong! it just crashes, and when I try to debug it, it just runs it and then says "program returned 3" even if I set a breakpoint!

here is my class:

Code: Select all

class Robot{
	unsigned char brain_code[BRAIN_SIZE];
	unsigned char ext_memory[EXT_MEM_SIZE];
	unsigned char mating_code[4]; //there is the initial one,the negated one, then the plus and minus 0x44 one
	int posx,posy;
	unsigned char hp;
	unsigned char attack_power;
	unsigned char rotation:2;

	public:
	bool turn;
	unsigned int r[8]; //r0-r7 for registers..
	unsigned int ip;
	Robot(int init_x,int init_y,const unsigned char *brain); //initial robots and mutations
	Robot(int init_x, int init_y,const Robot &parent1,const Robot &parent2);
	~Robot();
	//void Create(int init_x, int init_y, unsigned char *brain, unsigned char mate_code);
	unsigned int ChangeHp(signed int amount); //this is relational!
	unsigned int const GetHp(){return hp;}
	//It is constructed to bring to life and destructed to kill?
	int Move(signed int amount); //returns actual amount moved(in case something is in the way)
	void Rotate(bool way); //way is one for CW and zero for CCW
	char const GetDirection(){return rotation;} //returns RIGHT, LEFT, or UP or DOWN...
	//void Kill(); //this kills the Robot. Because of the way robots are linked in a list, we can't destruct it..
	void EndTurn(); //This decrements the hp for each turn
	void Attack(Robot &victim);
	unsigned char GetFrontPixel();
	unsigned char GetBackPixel();
	void Kill(void); //?
	unsigned char GetCurrentByte(){return brain_code[ip];}
	unsigned char GetByte(int pos){return brain_code[pos];}


};
and here is the only constructor I have implemented (and the only one I tested)

Code: Select all

Robot::Robot(int init_x,int init_y,const unsigned char *brain){
	cout <<"t";
	attack_power=5;

	memcpy(brain_code,brain,BRAIN_SIZE); //copy brain code
	if(display.GetPoint(init_x,init_y)!=FREESPACE_COLOR){
		/**Means that the requested coordinates are actually filled.**/
		cout << "Error: Requested coordinates for robot not valid!" << endl;
		exit(1);
	}
	rotation=UP;
	hp=HP_INIT;
	//have to setup matin gcode too!
}
and here is my simple test code:

Code: Select all

void DoRobot(){
	InitLogiCode();
	Robot cbot(5,2,test_brain); //works 
	Robot *tbot=new Robot(5,2,test_brain); //crashes
	//crobot=new Robot(5,2,test_brain);

	for(;;){
		//DoBrain(*crobot);
	}
}
Am I forced to override new? I really just don't see why I would have to..

Posted: Sat Jun 09, 2007 5:28 pm
by Kevin McGuire
Make sure test_brain is not null. If not then try commenting this block of code out in the constructor.

Code: Select all

if(display.GetPoint(init_x,init_y)!=FREESPACE_COLOR){
      /**Means that the requested coordinates are actually filled.**/
      cout << "Error: Requested coordinates for robot not valid!" << endl;
      exit(1);
} 

Posted: Sat Jun 09, 2007 5:42 pm
by earlz
Well, it never gets to the constructor! It somehow crashes before it!

like

Code: Select all


Robot::Robot(int init_x,int init_y,const unsigned char *brain){
	cout <<"t";
	attack_power=5;

	memcpy(brain_code,brain,BRAIN_SIZE); //copy brain code
	if(display.GetPoint(init_x,init_y)!=FREESPACE_COLOR){
		/**Means that the requested coordinates are actually filled.**/
		cout << "Error: Requested coordinates for robot not valid!" << endl;
		exit(1);
	}
	rotation=UP;
	hp=HP_INIT;
	//have to setup matin gcode too!
}
it never shows the "t"

Posted: Sat Jun 09, 2007 5:44 pm
by Alboin
hckr83 wrote:Well, it never gets to the constructor! It somehow crashes before it!

like

Code: Select all


Robot::Robot(int init_x,int init_y,const unsigned char *brain){
	cout <<"t";
	attack_power=5;

	memcpy(brain_code,brain,BRAIN_SIZE); //copy brain code
	if(display.GetPoint(init_x,init_y)!=FREESPACE_COLOR){
		/**Means that the requested coordinates are actually filled.**/
		cout << "Error: Requested coordinates for robot not valid!" << endl;
		exit(1);
	}
	rotation=UP;
	hp=HP_INIT;
	//have to setup matin gcode too!
}
it never shows the "t"
Try replacing the 'cout' with 'cerr'. That's the error stream, and should always print. (That is, if some thing's blocking cout.) If it prints then, it's something below it. Then again, that wouldn't explain why only a pointer isn't working...

Posted: Sat Jun 09, 2007 6:00 pm
by Kevin McGuire
Yes. Alboin is correct. I never knew that (about stderr operation). Just to add that if you do use the stdout stream then append a \n to the string to force the
stream to be flushed.

Also just for kicks: fflush(stdout).

Posted: Sat Jun 09, 2007 6:48 pm
by earlz
no, still doesn't print the "t" with cerr :(

really can get annoying sometimes...

the only weird thing is that the process always returns 0xFF. This has to do with anything? (and the test_brain isn't null)

Posted: Sat Jun 09, 2007 6:53 pm
by Alboin
Try initializing the pointer first. Maybe your class is messing something up...

Posted: Sat Jun 09, 2007 6:53 pm
by Kevin McGuire
Try putting a \n or doing a fflush(stderr) or fflush(stdout). This is a way that I know works for me on Linux.

Also try hanging the application with a:
for( ; ; );
After the debug message to give it time to print it.

Or, Attach (if you compile it for Linux):
The entire executable by compiling with no optimization.
A map file for the executable.

Posted: Sat Jun 09, 2007 6:59 pm
by earlz
tried it with no results..

and the pointer is initialized first..

Posted: Sat Jun 09, 2007 7:11 pm
by Alboin
This works on my system:

Code: Select all

#include <iostream>

using namespace std;

class Robot{
   public:char brain_code[78];
   bool turn;int attack_power;
   unsigned int r[8]; //r0-r7 for registers..
   unsigned int ip;
   Robot(int init_x,int init_y,string brain); //initial robots and mutations
   Robot(int init_x, int init_y,const Robot &parent1,const Robot &parent2);
   ~Robot(){}
};

#define BRAIN_SIZE 2
#define FREESPACE_COLOR 23
Robot::Robot(int init_x,int init_y,string brain){
   cout <<"t";
   attack_power=5;
} 


int main(){string test_brain = "meow";
  // InitLogiCode();
   Robot cbot(5,2,test_brain); //works
   Robot *tbot=new Robot(5,2,test_brain); //crashes
   //crobot=new Robot(5,2,test_brain);

   for(;;){
      //DoBrain(*crobot);
   }
}

?

Note: I changed to string, because it didn't want to compile with const unsigned char *'s.

Posted: Sat Jun 09, 2007 7:32 pm
by earlz
:( it's what I have begun to fear...
I use a second thread to automatically draw and to handle window messages...somehow, this causes it to crash with the pointer form of Robot...it's really weird, because the thread does nothing with Robot, and in the main thread, the Robot constructor does nothing with the Screen class(the only class used in the second thread)

umm...yea...guess I'll have to fix this alone :( my code is too extensive to just jump into...(plus I'm a new C++ programmer, so my style isn't the best)

Posted: Sat Jun 09, 2007 7:52 pm
by Kevin McGuire
I have a very similar problem when using the POSIX pthread library and SDL. It seems when I turn on optimization it likes to crash, and I am unable to find the problem.

Posted: Sat Jun 09, 2007 8:42 pm
by earlz
what's really stupid is I'm usign SDL threads! I know it said to "not use threading and video" but, God! I think that's just a bit stupid..I've already eliminated what I thought were all of the threading problems that could occur! I mean, I had to use my own double buffer, because if you write to SDL_Surface->pixels in another thread, it will crash..(even volatile!) ...bleh!! stupid SDL...

Posted: Sat Jun 09, 2007 9:11 pm
by Alboin
Can't you just add the SDL video update into the main loop? Usually, threads aren't necessary, and can be fixed using a different method.

Posted: Sat Jun 09, 2007 9:38 pm
by earlz
Well, it's just inconvienent, plus I'd like to be able to take advantage of my dual-core...
but what I hate about messaging systems is that it doesn't tell you when you get a message, you just have to periodically check! I think this is annoying and not well designed....And if your program is busy doing something or you forget to check the messages in some place where it could be a long time before getting to another "checkpoint" then, when you try to exit out of the program, it will say the program is not responding(which it isn't) on windows.....I hate message based systems..


but I could change it to nto use threads, just have to recode a lot of crap.. :evil: