I'm making a game with bullet physics where I have a Platform class, which contains a btBoxShape, btDefaultMotionState and a btRigidBody as members. When exiting my game it crashes. I store the Platform objects in an std::vector, and I think this is the problem. When I push back more Platforms, the vector resizes and moves in memory. This means that whatever pointer to the shape and motionstate that the rigid body has is invalidated. How should I solve this?
Asked
Active
Viewed 183 times
2 Answers
2
A std:deque will not invalidate the references.
0
There are a couple of options I can think of.
std::array
/std::vector
with a fixed size(reserve()
), if possible
std::vector<Platform*>
You should probably use smart pointers here, but basically allocate yourself and store just pointers in the vector
.

Karthik T
- 31,456
- 5
- 68
- 87
-
I'm not the one storing pointers, Bullet does it. Its objects need pointers to related objects. – Caroline Bengtsson Aug 17 '13 at 14:24
-
@CarolineBengtsson Oh I see – Karthik T Aug 17 '13 at 14:25
-
@CarolineBengtsson Does my new answer help any better? – Karthik T Aug 17 '13 at 14:28