Advertisement

Crazy problem with STL vectors(help plz)

Started by April 15, 2001 12:09 PM
3 comments, last by GalaxyQuest 23 years, 9 months ago
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 Object
 
I then have a method in my game which is called createObject(...) // this adds a Object to vector When (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 members
vector::iterator p = objectContainer.end();

// end() points to one past the last element, so decrement to get last
p--;
	
// now, finish INIT of object
p->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!! I know this is far fetched, but if anyone has any clues to what is going on i would appreciate the help. thanks. Visit my planet: Planet John Capt. James Tiberious Kirk -- hmm, didnt know ole Capn was a tiberia fan. Edited by - GalaxyQuest on April 16, 2001 12:22:49 AM
I am wondering if, because I have "array''s" within my Object class which I have to dynamically allocate after creating and storing this object in a vector is my problem.

Of course, i just love talking to my self....

Visit my planet: Planet John
Capt. James Tiberious Kirk -- hmm, didnt know ole Capn was a tiberia fan.
Advertisement
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!!

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.
Hey stoffel, thanks for the reply and email.

I dont really know why u cant see the message ok, i can view it alright.

Anyways, my first implementation was just an array of *Object''s. I decided to test out a vector implementation and the results are what i posted. I used up an entire day learning STL and so i dont consider it a complete lost, though 1 day is like a week for me since i really only get one real day to work on this, so for now I am going back to my first (and really simple) solution for now.

The vector problem can possibly be your suggestion about the copy constructor, since I do not have one (all my object''s were created dynamically "new" using an array of *object). So either it *is* your point or something related: I dynamically allocate arrays within the class *after* object creation.

It could be either or both, but for now, it will have to remain a mystery unless i find some time, which I doubt...i have to move on with this school project.

Anyways, one last question, what are these services packs you mention, im not up to speed on my VC++ news and stuff??

and, once again, thanks for your points.

Visit my planet: Planet John
Capt. James Tiberious Kirk -- hmm, didnt know ole Capn was a tiberia fan.
I recall having a problem a bit like this before. Vectors are kinda pissy if you don''t treat ''em right! :-)

Firstly, if you''re only going to allocate your objects once, just use a dynamic array and be done with it. Vectors are only nessessary if the number of objects is going to change during your program.

So if you''re going to be managing a changing number of objects, Use a vector of pointers to your objects, instead of a vector of actual object instances. It''d sure be nice if the vector class could handle that for you, but it tends to choke on things like that, probably for the reasons Stoffel was talking about.
So when you need a new object, allocate one with new, and call its init methods via the newly created pointer:

vector.push_back(new object);
vector[vector.size() - 1]->mSprite.loadAnimation(0, numFrames, frames);
etc etc etc;

That might not be the ideal way to manage things, but that''s what I do and it works pretty good.

This topic is closed to new replies.

Advertisement