Say I have 2 classes -
class BulletCollection;
class Bullet;
BulletCollection is responsible for instantiating / destroying Bullet objects by allocating new Bullet instances and storing the pointer in its std::map<int, Bullet*> so it can be iterated through, etc. Looks something like:
Bullet b* = new Bullet( id, in_ttl ); // id is next available id, gotten elsewhere blist[id] = b; // blist is a std::map<int, Bullet*>, member of BulletCollection
Bullet is, well, a bullet. Has a TTL (time to live), which decreases when Bullet::update() is called. When TTL < 0, it will call BulletCollection::killBullet( id ); See, in my class Bullet I store a BulletCollection* BC that points to the BulletCollection it belongs to. So my Bullet::update() looks like this:
Bullet::update() {
TTL--;
if (TTL < 0) {
BC->killBullet ( this.id );
}
}
My problem is my BulletCollection::killBullet() is giving me weird issues (crashes).
And in my BulletCollection::killBullet( in_id ) I have something like:
BulletCollection::killBullet( in_id ) {
delete blist[in_id]; // frees the memory associated with the pointer
blist[in_id].erase(); // erases the entry from my std::map blist
}
... as I just typed that, I realize a potential problem... should I be doing:
delete *blist[in_id]; instead of delete blist[in_id]; ???
My entire code works and keeps running if my Bullet TTL is infinitely large, so I know it's a problem with entity destruction. Can someone please give me some insight?












