function just works sometimes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gregor Peter

    function just works sometimes

    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(Fr ame(Location(0, 400,130),Orient ation(0,90,-90)),110 );
    //r2d2.moveP2P(Fr ame(Location(0, 600,300),Orient ation(0,90,-90)),110 );
    //
    //// End MARK I ////

    // next line cause the crash
    threeCPO.moveP2 P(Frame(Locatio n(0,550,330),Or ientation(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

    ---

    To eMail me remove all 'U' from the following adress:
    gpUts@gmUx.de





  • Attila Feher

    #2
    Re: function just works sometimes

    Gregor Peter wrote:
    [SNIP][color=blue]
    > 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;)[/color]

    Do you see terminate in that stack trace?

    --
    Attila aka WW


    Comment

    • Gregor Peter

      #3
      Re: function just works sometimes



      Attila Feher wrote:[color=blue]
      > Gregor Peter wrote:
      > [SNIP]
      >[color=green]
      >>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;)[/color]
      >
      >
      > Do you see terminate in that stack trace?
      >
      > --
      > Attila aka WW
      >
      >[/color]

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

      Gregor

      Comment

      • Karl Heinz Buchegger

        #4
        Re: function just works sometimes



        Gregor Peter wrote:[color=blue]
        >
        > 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 );[/color]

        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.

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • WW

          #5
          Re: function just works sometimes

          Gregor Peter wrote:[color=blue]
          > Attila Feher wrote:[color=green]
          >> Gregor Peter wrote:
          >> [SNIP]
          >>[color=darkred]
          >>> 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;)[/color]
          >>
          >>
          >> Do you see terminate in that stack trace?
          >>[/color]
          >
          > Two of them '__terminate' from '/lib/librobot.so'[/color]

          You have an exception thrown but not caught.

          --
          WW aka Attila


          Comment

          • Gregor Peter

            #6
            Re: function just works sometimes (Update)



            Is it possible to have a problem with different constructors?

            The class robot has two constructors


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

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


            Robot::Robot(va lue1, value2, value3, value4, value5, value6 ):CKR6{
            memberVar1OfRob ot = value1;
            memberVar2OfRob ot = value2;
            memberVar3OfRob ot = value3;
            memberVar4OfRob ot = value4;
            memberVar5OfRob ot = value5;
            memberVar6OfRob ot = 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(vo id):CKR6{
            memberVar1OfRob ot = -90;
            memberVar2OfRob ot = 100;
            memberVar3OfRob ot = 90;
            memberVar4OfRob ot = 20;
            memberVar5OfRob ot = 90;
            memberVar6OfRob ot = -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

            ---

            To eMail me remove all 'U' from the following adress:
            gpUts@gmUx.de




            Comment

            • Gregor Peter

              #7
              Re: function just works sometimes



              Karl Heinz Buchegger wrote:[color=blue]
              >
              >
              > 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.
              >[/color]
              There is no poerator= (yet)!

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

              Comment

              • Gregor Peter

                #8
                Re: function just works sometimes



                Karl Heinz Buchegger wrote:

                [color=blue]
                > 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.[/color]


                There is no oerator= (yet)!

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

                Comment

                Working...