function just works sometimes

G

Gregor Peter

Hi folks,

I've got a problem with calling a memberfunction from a inheriteded class.
It seems to work sometimes and sometimes not.

There is a Class 'Robot' that is derived from a foreigen class named 'CKR6'.
As far as I can say both of them worked fine till today.
The function I wanna use is 'moveP2P' from 'Robot'
that mainly calls 'movePTP' from class 'CKR6'.

Deklaration in file robot.h :
-----------------------------
Class Robot : CKR6 {
...
void moveP2P( const Frame& point, const int& status );
...
} // end Class robot : CKR6



Definition in file robot.cpp :
------------------------------
void Robot::moveP2P( const Frame& point, const int& status ){
...
CKR6::movePTP( calcJointAngles( point, status ));
...
} // moveP2P



I wrote a Programm 'hanoigame' that uses class 'robot' and it's
function 'moveP2P'.

Deklaration in file 'hanoigame.h'
-----------------------------------
class HanoiGame {

private:
Tower westTower, midTower, eastTower;
Robot threeCPO;//Deklaration of variable used in 'hanoigame.cpp'

public: HanoiGame();// std ctor
public: ~HanoiGame();//dtor moves the ropot back to home position
public: void start();// starts the game Towers of Hanoi

/** move as many discs as specifiyed
from the start- to destination-tower */
public: void move( Tower& start, Tower& help, Tower& destination,
const int& quantityDiscs );

}; // end CLASS HanoiGame


The constuctor of 'hanoigame' looks like this
(Code originaly does not contain the stuff sourrounded by 'MARK I')


Definition of 'hanoigame's ctor in 'hanoigame.cpp'
--------------------------------------------------
// creation of discs and stacking them on a tower
...

// instanciating of robot 'threeCPO' declared in 'hanoigame.h'
threeCPO = Robot( value1, value2, value3, value4, value5, value6 );

//// Begin MARK I ////
//
//Robot r2d2( coord1, coord2, coord3, coord4, coord5, coord6 );
//
//r2d2.moveP2P(Frame(Location(0,400,130),Orientation(0,90,-90)),110 );
//r2d2.moveP2P(Frame(Location(0,600,300),Orientation(0,90,-90)),110 );
//
//// End MARK I ////

// next line cause the crash
threeCPO.moveP2P(Frame(Location(0,550,330),Orientation(0,90,-90)),110 );
cerr << " Position reached!" << endl;



When I call the function 'movePTP' from class 'robot' the program
crashes. The debugger tells me that there are called some functions I've
never been seen before and that end up in
'kill()' from '/lib/libc.so.6'.
For me thats means my program runs into nirwana .... (and does not come
back;)

The strange thing is if I uncomment the statements in 'MARK I' it works
fine.
In 'MARK I' I declare and define second robot 'r2d2' and let it do some
movements.

I don't want to create always a temporaly robot when 'threeCPO' has to
move. This won't work anyway cause in other functions beyond the ctor
'hanoigame' crashes in the same way even when the 'MARK I' stuff is
uncommented in the ctor of 'hanoigame'.


Can anyone explain me why this happens?
(And tell me how to solve this problem?)

Thanks for reading this

G. Peter
 
A

Attila Feher

Gregor Peter wrote:
[SNIP]
When I call the function 'movePTP' from class 'robot' the program
crashes. The debugger tells me that there are called some functions
I've never been seen before and that end up in
'kill()' from '/lib/libc.so.6'.
For me thats means my program runs into nirwana .... (and does not
come back;)

Do you see terminate in that stack trace?
 
G

Gregor Peter

Attila said:
Gregor Peter wrote:
[SNIP]
When I call the function 'movePTP' from class 'robot' the program
crashes. The debugger tells me that there are called some functions
I've never been seen before and that end up in
'kill()' from '/lib/libc.so.6'.
For me thats means my program runs into nirwana .... (and does not
come back;)


Do you see terminate in that stack trace?

Two of them '__terminate' from '/lib/librobot.so'

Gregor
 
K

Karl Heinz Buchegger

Gregor said:
Hi folks,


Definition of 'hanoigame's ctor in 'hanoigame.cpp'
--------------------------------------------------
// creation of discs and stacking them on a tower
...

// instanciating of robot 'threeCPO' declared in 'hanoigame.h'
threeCPO = Robot( value1, value2, value3, value4, value5, value6 );

threeCPO is already a Robot, it is already instantiated.
The above createas a new Robot one and assigns it to threeCPO, thereby
destroying the previous Robot.

This begs the question:
Did you check that copy assignment works for class Robot and it's
parent classes, if you have not written an operator= on your own
or did you write an operator= on your own and is this one correct.

Basically: if a class does some memory management on its own (new, delete)
then you *must* write an operator= on your own as the one generated by
the compiler does the wrong thing.
 
W

WW

Gregor said:
Attila said:
Gregor Peter wrote:
[SNIP]
When I call the function 'movePTP' from class 'robot' the program
crashes. The debugger tells me that there are called some functions
I've never been seen before and that end up in
'kill()' from '/lib/libc.so.6'.
For me thats means my program runs into nirwana .... (and does not
come back;)


Do you see terminate in that stack trace?

Two of them '__terminate' from '/lib/librobot.so'

You have an exception thrown but not caught.
 
G

Gregor Peter

Is it possible to have a problem with different constructors?

The class robot has two constructors


Definition in file robot.cpp :
------------------------------

Robot::Robot(void):CKR6{
// nothing;
} // ctor 1


Robot::Robot(value1, value2, value3, value4, value5, value6 ):CKR6{
memberVar1OfRobot = value1;
memberVar2OfRobot = value2;
memberVar3OfRobot = value3;
memberVar4OfRobot = value4;
memberVar5OfRobot = value5;
memberVar6OfRobot = value6;
} // ctor 2


void Robot::moveP2P( const Frame& point, const int& status ){
...
CKR6::movePTP( calcJointAngles( point, status ));
...
} // moveP2P


In the headerfile of hanoi game I tell the compiler to reserve some
memory for var of type robot by declaring 'threeCPO'


Deklaration in file 'hanoigame.h'
-----------------------------------
class HanoiGame {

private:
Robot threeCPO; //Deklaration of variable used in
'hanoigame.cpp'

...

}; // end CLASS HanoiGame


In the source file 'hanoigame.cpp' I specify how to instanciate the
robot by calling it's constructor.


Definition of 'hanoigame's ctor in 'hanoigame.cpp'
--------------------------------------------------
...

// instanciating of robot 'threeCPO' declared in 'hanoigame.h'
threeCPO = Robot( -90, 100, 90, 20, 90, -90 );



If I delete this line in 'hanoigame.cpp' and rewrite the 1st ctor of
robot in file 'robot.cpp' like this

(Re)Definition of 1st ctor in file robot.cpp :
----------------------------------------------

Robot::Robot(void):CKR6{
memberVar1OfRobot = -90;
memberVar2OfRobot = 100;
memberVar3OfRobot = 90;
memberVar4OfRobot = 20;
memberVar5OfRobot = 90;
memberVar6OfRobot = -90;
} // ctor 1

everything is fine.

But Why?


I thought it is the use of a constuctor to put the declaration of a
member variable into the class declaration ('hanoigame.h') and the
creation of a instance into the implementation (ctor of hanoigame in
'hanoi.cpp') of the using program?



G. Peter
 
G

Gregor Peter

Karl said:
This begs the question:
Did you check that copy assignment works for class Robot and it's
parent classes, if you have not written an operator= on your own
or did you write an operator= on your own and is this one correct.
There is no poerator= (yet)!

I'll gonna fix that.
That gives me something to work on, thanks ;-)
 
G

Gregor Peter

Karl Heinz Buchegger wrote:

This begs the question:
Did you check that copy assignment works for class Robot and it's
parent classes, if you have not written an operator= on your own
or did you write an operator= on your own and is this one correct.


There is no oerator= (yet)!

I'll gonna fix that.
That gives me something to work on, thanks ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top