So, I have a Sprite object which has a 2D vector "coll_box" as seen below. It should hold some Vector3 pointers (x,y,z). Every row should contain info. on different "collision rectangles" (4 points).
For now I just want to add 4 dots int the first row, but I am not able to "push_back" Vector3, in the vector. The program just crashes.
The most likely reason for the crash would be memory access violation at
a->coll_box[0]
[font="Arial"]when a->coll_box is empty.
If you just want to add a new set of Vector3 pointers to a->coll_box, you will need to do a 2-level push_back: one at std::vector <Vector3*> level, then at [/font][font="Arial"]std::vector[/font] < [font="Arial"]std::vector <Vector3* > > level.
Create a temporary variable A of type [/font][font="Arial"]std::vector <Vector3*>, push_back the new-ed Vector3 object pointers into variable A, and then finally push_back A into a->coll_box.[/font]
[color=#1C2837][size=2]Does your vector<Vector *> need to be a pointer?
[color=#1C2837][size=2]
[color=#1C2837][size=2]Wouldn't it be a lot easier to just have [color=#1C2837][size=2]std::vector < std::vector <Vector3> > coll_box;
[color=#1C2837][size=2]
[color=#1C2837][size=2]It would save you from the "new" and "delete" pain.
[color=#1C2837][size=2]
Do you need to use pointers? Can you store them by value? That would simplify everything, and probably speed up your program too.
Should I go trough the vector elements and delete them...
[/quote]
If you're allocating them with new, yes you must delete them.
... and then call vector::clear (twice )?
[/quote]
If you clear the outer vector, you are removing all the inner vectors, so you don't need to manually clear them. You shouldn't need to clear anything twice.
Do you need to use pointers? Can you store them by value? That would simplify everything, and probably speed up your program too.
Should I go trough the vector elements and delete them...
If you're allocating them with new, yes you must delete them.
... and then call vector::clear (twice )?
[/quote]
If you clear the outer vector, you are removing all the inner vectors, so you don't need to manually clear them. You shouldn't need to clear anything twice.
[/quote]
[offtopic]
If you forget to call the delete the OS will still clean it up though after the process has terminated, this doesn't help with your memory situation while running your app though.
[/offtopic]
You shouldn't use clear any way after you called delete on your vectors use the swap trick to set your vectors back to using no memory and let the old one scope out in the temporary var you just created.