From ba48fe2f4e8bbcf53dbf66c40503a5751354f379 Mon Sep 17 00:00:00 2001 From: Jack Palevich Date: Fri, 27 Nov 2009 11:51:36 +0800 Subject: [PATCH] Fix memory deallocation bug in toy vector class. Yet another reason to use standard collection classes. :-) --- libacc/acc.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libacc/acc.cpp b/libacc/acc.cpp index 22cfb55f7..8f33b0bdd 100644 --- a/libacc/acc.cpp +++ b/libacc/acc.cpp @@ -82,9 +82,7 @@ template class Vector { ~Vector() { if (mpBase) { - for(size_t i = 0; i < mUsed; i++) { - mpBase[mUsed].~E(); - } + clear(); free(mpBase); } } @@ -110,10 +108,20 @@ template class Vector { * ensure(1) = item; } - size_t size() { + inline size_t size() { return mUsed; } + void clear() { + if (mpBase) { + size_t used = mUsed; + for(size_t i = 0; i < used; i++) { + mpBase[i].~E(); + } + } + mUsed = 0; + } + private: E* ensure(int n) { size_t newUsed = mUsed + n;