Skip to content

Commit

Permalink
UPBGE: Securise std::vector erase iteration.
Browse files Browse the repository at this point in the history
Using a fixed end iterator or a incrementing the iterator after
delete its previous is unsafe for std::vector. Because erasing
an element can realloc the whole container and then invaldate
the existing iterators.

To fix this issue we compare with the current returned of .end()
every frame and assign the iterator to the returned value of
.erase instead of incrementing the iterator.
  • Loading branch information
panzergame committed Sep 25, 2016
1 parent ef7fbd8 commit 2c669cc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions source/gameengine/Rasterizer/RAS_BucketManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,10 @@ void RAS_BucketManager::RemoveMaterial(RAS_IPolyMaterial *mat)
{
for (unsigned short i = 0; i < NUM_BUCKET_TYPE; ++i) {
BucketList& buckets = m_buckets[i];
for (BucketList::iterator it = buckets.begin(), end = buckets.end(); it != end; ) {
for (BucketList::iterator it = buckets.begin(); it != buckets.end();) {
RAS_MaterialBucket *bucket = *it;
if (mat == bucket->GetPolyMaterial()) {
buckets.erase(it++);
it = buckets.erase(it);
if (i == ALL_BUCKET) {
delete bucket;
}
Expand Down
4 changes: 2 additions & 2 deletions source/gameengine/Rasterizer/RAS_MaterialBucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ void RAS_MaterialBucket::RemoveMesh(RAS_MeshSlot *ms)

void RAS_MaterialBucket::RemoveMeshObject(RAS_MeshObject *mesh)
{
for (RAS_MeshSlotList::iterator it = m_meshSlots.begin(), end = m_meshSlots.end(); it != end;) {
for (RAS_MeshSlotList::iterator it = m_meshSlots.begin(); it != m_meshSlots.end();) {
RAS_MeshSlot *ms = *it;
if (ms->m_mesh == mesh) {
delete ms;
m_meshSlots.erase(it++);
it = m_meshSlots.erase(it);
}
else {
++it;
Expand Down

0 comments on commit 2c669cc

Please sign in to comment.