Game Developer




The Gang Of Three

Category : Win8 Dev · by Jul 9th, 2012

Shame on me. I was wondering, why my Sprite data is corrupted after copying to vector class, which was causing Debug assertion failed – vector iterator is not dereferencableerror.

And that is why. Because I have not implemented copy constructor for Sprite class this code was created a problem:

  m_alienShipList.push_back(AlienShip(position, speed, m_screeSize, m_alienShipTexture)); 

how it is working.

  • First, copy of AlienShip object is created as temporary.
  • It was passed to vector class m_alienShipList, which fired copy constructor.
  • After it has been copied, old temporary valued is deleted.

Because shallow copy has been made, I was deleting the memory I was trying to use after.

I had now it is true for Pointers, but not for Vectors. But seems vector is acting in similar manner to pointer.

My knowledge extended from

When use Pointer use Gang Of Three

to

When use Pointer OR Vector use Gang Of Three

EDIT:

Problem appears to be even deeper. I was storing vector<QuadSheet>::iterator inside each Sprite. Iterator is very similar to pointer. But problem is, every time I add new Object to vector, old Iterator became invalid. And so, I can’t rely on that, and of course, I just can’t allow it to be copied to another object.

Every day I see more necessary to create my own list class.

SHARE :

Leave a Reply

Your email address will not be published.