Invalid Pointer - No help needed just fun facts

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
Post Reply
DarkBoss
Halfling
Posts: 56
Joined: Tue Sep 08, 2009 6:37 pm

Invalid Pointer - No help needed just fun facts

Post by DarkBoss »

Hey,
i just wanted to report something funny that happened to me, well not sure if that's fun for you but when i realised i was kinda smiling :)

here it goes:
i had a class "SceneState" which represents all the entities in the current scene ( to save it to disk later ). Everything went fine and i used Ogre::SceneManager::MovableObjectIterator to get those entities.
In this class i had a std::vector<Ogre::Entity*>.
In my "SceneBuilder" module i declared a SceneState variable ( no pointer juste normal variable ).
And it ran fine. Then later, for "i dunno why i did that", i changed this variable from normal to pointer ( which i should have allocated with new and deleted at the end ).
Well after changing the get/setter methods i forgot to alloc it at init etc.

I kept on calling the function inside that class that was meant to build the scene state.
And i got NO PROBLEM calling it ... like if the method was static...
well in fact i got no problem as long as it didn't involve the vector's push_back method. But i could call QMessageBox( QT ) append to a QTextEdit control etc.

And when i realized i havent alloced the pointer i was really amazed because i know when u dont alloc a pointer nor initialize it on NULL it can point on any place in memory..
but how many chance of that to exist in reality ? i mean there was only one variable of this type in all my code, could the compiler kind of allocated for me ?
Is this even possible ? Not to initialize a pointer and get a 'good pointer' which class' methods were working ?

Now that i allocated it it fully works, vector's push back causes no more "Unhandled exception". I retried to not allocate it just to see. and now it crashes before entering the function so no more "bad-good pointer".
Could it be due to the fact that somehow my normal variable was still in memory ? and in the same exact adress ?

well that's all freakyness for today but it's just a fact that really surprised me ! And a behavior that by 8 years of c++ have never encountered.
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts: 2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
x 220
Contact:

Re: Invalid Pointer - No help needed just fun facts

Post by Jabberwocky »

I'm not a master of compilers or low level assembly or anything. But your random pointer probably worked because only the data contained by your class would be random. The functions wouldn't. It's not like your computer makes a new mem-copy of all the functions for each variable that gets declared***. So your functions will be good, but your variables bad, I think. It wasn't until you started messing around with the std::vector data that the bad memory caused an issue. Each time one of your class functions accesses some of that class instance's data, that's when you're rolling the dice into random memory.

*** a class with virtual functions is a little different, the vtable needs to route things to the correct virtual function.
Image
DarkBoss
Halfling
Posts: 56
Joined: Tue Sep 08, 2009 6:37 pm

Re: Invalid Pointer - No help needed just fun facts

Post by DarkBoss »

ok thanks for this clarification. it all makes sense now.

But now it's less less fun than i imagined in the first place :(.
i dunno why in my mind the methods were allocated per instance not per class type,
and i didn't even asked myself if it was the case, i guess it's one of those "i know it, but i dunno where it comes from"

anyway good evening sir ;).
Post Reply