quote:
Original post by GalaxyQuest
Ok, i am using visual c++ 5.0 pro and decided to go with STL ''s for storing objects that exists in the game map (this is my senior project)...so I have a vector of "class Object" like so:vector objectContainer; // empty vector of type ObjectI then have a method in my game which is called createObject(...) // this adds a Object to vectorWhen (createObject(...)) is called, it "pushes back" a new Object like so:objectContainer.push_back( Object(...) );I also need to set up some stuff with this new object so i then access it like so:// lets get this object again to INIT more of Object''s membersvector::iterator p = objectContainer.end();// end() points to one past the last element, so decrement to get lastp--; // now, finish INIT of objectp->mSprite.loadAnimation(0, numFrames, frames);p->mSprite.setMaxFrameX(max);p->mSprite.setAnimation(0); p->setPosition(pos); p->setType(type);p->setState(state);
Ok, so far so good, but here is where the weird shit starts.
**If I add just 1 object at runtime to the vector everything is fine( no assertion failure during runtime or at exit).
**When I add just 2 objects at runtime to vector, when game closes, i get a "Debug Assertion failed" (in dbgheap.c) at expression
_CrtlsValidHeapPointer(pUserHeap).
**When I add "try" to add 3(or more) objects at runtime to vector, at START, game crashes with above error.
This is totally insane, i get 3 different responses to 3 different runtime states!! ![](sad.gif)
I know this is far fetched, but if anyone has any clues to what is going on i would appreciate the help. thanks.
Well, first of all, since your class is named "Object", the board tried to do something really funky with your post. =) I''ve re-quoted it so we can all see it.
Tip 1: use back () instead of end () and then decrementing. That''s just hokey & error-prone, not to mention ineffecient (though it might compile to the same code). Example:
objectContainer.push_back (Object (...));Object &obj = objectContianer.back (); // returns ref to last objectobj.mSprite.loadAnimation(0, numFrames, frames); // you have public data? SHAME!!
|
Tip 2: Since you''re using a vector of Object instead of a vector of Object*, Object exists in-size in the vector. This means that:
- you must have a working copy constructor and should have a working assignment operator.
- you cannot place any objects derived from Object in the container since they will be a different size than the base class Object. If you''re trying to use Object polymophically, you need to make it a vector of Object*, use new to insert and assure that you delete when you erase.
Tip 3: STL in MSVC is completely screwed unless you have at least service pack 3 in version 5.0 and 6.0. Make sure you have the latest service pack.
If this doesn''t help you fix your problem, I''ll need the following to help:
- the Object constructors, copy constructors, assignment operators and destructor.
- everywhere you use objectContainer.