Skip to content

Commit

Permalink
prevector: destroy elements only via erase()
Browse files Browse the repository at this point in the history
Fixes a bug in which pop_back did not call the deleted item's destructor.

Using the most general erase() implementation to implement all the others
prevents similar bugs because the coupling between deallocation and destructor
invocation only needs to be maintained in one place.
Also reduces duplication of complex memmove logic.
  • Loading branch information
kazcw committed Apr 16, 2016
1 parent 73fc922 commit 1e2c29f
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/prevector.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,8 @@ class prevector {
}

void resize(size_type new_size) {
while (size() > new_size) {
item_ptr(size() - 1)->~T();
_size--;
if (size() > new_size) {
erase(item_ptr(new_size), end());
}
if (new_size > capacity()) {
change_capacity(new_size);
Expand Down Expand Up @@ -368,10 +367,7 @@ class prevector {
}

iterator erase(iterator pos) {
(*pos).~T();
memmove(&(*pos), &(*pos) + 1, ((char*)&(*end())) - ((char*)(1 + &(*pos))));
_size--;
return pos;
return erase(pos, pos + 1);
}

iterator erase(iterator first, iterator last) {
Expand All @@ -396,7 +392,7 @@ class prevector {
}

void pop_back() {
_size--;
erase(end() - 1, end());
}

T& front() {
Expand Down

0 comments on commit 1e2c29f

Please sign in to comment.