CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2009
    Posts
    18

    Question strange: dynamic casting and free(): invalid pointer

    Basically i get a "free(): invalid pointer: 0x00000000006de9b8" error. However it's not because i'm deleting the wrong pointer. The pointer was properly created with "new" and the one created and the one being deleted do match.

    The pointer i'm deleting is a pointer type of an abstract base class while it is actually pointing to an object of a derived class.

    When I cast the pointer down to the actual derived type it does delete properly. Why? Please elaborate on the issue.

    I'm not going to paste the complete code here because of it's size. I have tried to reproduce the error with a simpler example but have failed.

    Code:
    class Game{
    public:
    	virtual void start() = 0;
    	virtual void pauze() = 0;
    	virtual void terminate() = 0;
    
    	virtual void addGameStatusObserver(GameStatusObserver * observer) = 0;
    	virtual void removeGameStatusObserver(const GameStatusObserver * observer) = 0;
    };
    Code:
    Game *MyGameFactory::createGame() {
    
    	EngineFactory engineFactory;
    	GameEngineFacade enf = engineFactory.createGameEngine();
    	AIRacket racket = AIRacket();
    	MouseRacket mouseRacket = MouseRacket();
    	Game *game = (Game *)(new GameWorldFacade(enf, racket, mouseRacket));
    	return (Game *)game;
    }
    Code:
    GameFrameWork::~GameFrameWork() {
    	for (unsigned int i = 0; i < games.size(); i++) {
    		//GameWorldFacade * game = (GameWorldFacade *)games[i]; // ok
    		Game * game = games[i];  // invalid pointer:
    		game->terminate();
    		delete game; // runtime error on delete
    	}
    }

  2. #2
    Join Date
    Sep 2003
    Posts
    213

    Re: strange: dynamic casting and free(): invalid pointer

    Hi
    Base on the code snippets I can't tell what is wrong also.
    The pointer i'm deleting is a pointer type of an abstract base class while it is actually pointing to an object of a derived class
    You should have a virtual destructor at your base class which is your Game class.
    Code:
    virtual ~Game();
    Might not solve the problem though..it just ensure proper clean up during your object destruction

  3. #3
    Join Date
    Mar 2009
    Posts
    18

    Resolved Re: strange: dynamic casting and free(): invalid pointer

    Thanks. That solved the issue. I feel that the compiler could have picked up on this.

    However the question remains why i can't recreate the runtime error with a simple example. Is the behavior of the delete instruction dependent on the nature of the class?

    Code:
    class Base
    {
           public:
              Base(){cout<<"Constructor: Base"<< "\n";}
              //virtual ~Base(){ cout<<"Destructor : Base"<<"\n";} //Why doesn't this cause a runtime error?
    };
    class Derived: public Base
    {
           public:
               Derived(){ cout<<"Constructor: Derived"<<"\n";}
               ~Derived(){ cout<<"Destructor : Derived"<<"\n";}
    };
    
    int main() {
        Base *Var = new Derived();
        delete Var;
    }

  4. #4
    Join Date
    Oct 2008
    Posts
    1,456

    Re: strange: dynamic casting and free(): invalid pointer

    Why doesn't this cause a runtime error?
    it just ensure proper clean up during your object destruction
    NO, deleting a pointer to the base of a polymorphic type with non virtual destructor is UNDEFINED BEHAVIOUR !

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: strange: dynamic casting and free(): invalid pointer

    Quote Originally Posted by superbonzo
    NO, deleting a pointer to the base of a polymorphic type with non virtual destructor is UNDEFINED BEHAVIOUR !
    I would not say "NO", but rather "more accurately". Even more accurately, I would say that the undefined behaviour happens when delete is used on a pointer to the base class which points to an object of a derived class. Since the base class here is an abstract base class, the distinction is not so relevant, except that there would still be no effect if the pointer is a null pointer.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Oct 2008
    Posts
    1,456

    Re: strange: dynamic casting and free(): invalid pointer

    I would say that the undefined behaviour happens when delete is used on a pointer to the base class which points to an object of a derived clas
    yes, off course I meant that it have to points to an instance of the derived type; my 'NO' referred to the last quote in my post... anyway, thanks for precising that...

  7. #7
    Join Date
    Sep 2003
    Posts
    213

    Re: strange: dynamic casting and free(): invalid pointer

    Thanks for the correction I didnt know it will result in undefine behavior. Still have a long way to learn

  8. #8
    Join Date
    Mar 2009
    Posts
    18

    Resolved Re: strange: dynamic casting and free(): invalid pointer

    Thanks. Don't you just hate undefined behavior :-). To bad the gcc compiler doesn't even give a warning.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured