-
April 9th, 2009, 06:56 PM
#1
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
}
}
-
April 9th, 2009, 10:55 PM
#2
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.
Might not solve the problem though..it just ensure proper clean up during your object destruction
-
April 10th, 2009, 03:51 AM
#3
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;
}
-
April 10th, 2009, 03:56 AM
#4
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 !
-
April 10th, 2009, 04:21 AM
#5
Re: strange: dynamic casting and free(): invalid pointer
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.
-
April 10th, 2009, 04:38 AM
#6
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...
-
April 10th, 2009, 05:05 AM
#7
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
-
April 10th, 2009, 05:33 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|