diff options
author | Max Horn | 2009-05-03 22:45:31 +0000 |
---|---|---|
committer | Max Horn | 2009-05-03 22:45:31 +0000 |
commit | 882c24d2ee9f71640bfc3be811b8dcddd91c9988 (patch) | |
tree | 50ebb0fea1e40cd922d0660588ba481141c9e49c /common/str.cpp | |
parent | f108a31ad71404f115de776366dae985dd8e4f48 (diff) | |
download | scummvm-rg350-882c24d2ee9f71640bfc3be811b8dcddd91c9988.tar.gz scummvm-rg350-882c24d2ee9f71640bfc3be811b8dcddd91c9988.tar.bz2 scummvm-rg350-882c24d2ee9f71640bfc3be811b8dcddd91c9988.zip |
COMMON: Check for failed memory allocations; changed Common::String to use new/delete instead of malloc/free
svn-id: r40291
Diffstat (limited to 'common/str.cpp')
-rw-r--r-- | common/str.cpp | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/common/str.cpp b/common/str.cpp index 10d258e058..abe0ab6650 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -79,7 +79,7 @@ void String::initWithCStr(const char *str, uint32 len) { // Not enough internal storage, so allocate more _extern._capacity = computeCapacity(len+1); _extern._refCount = 0; - _str = (char *)malloc(_extern._capacity); + _str = new char[_extern._capacity]; assert(_str != 0); } @@ -159,7 +159,7 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) { newCapacity = MAX(curCapacity * 2, computeCapacity(new_size+1)); // Allocate new storage - newStorage = (char *)malloc(newCapacity); + newStorage = new char[newCapacity]; assert(newStorage); } @@ -190,8 +190,10 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) { void String::incRefCount() const { assert(!isStorageIntern()); if (_extern._refCount == 0) { - if (g_refCountPool == 0) + if (g_refCountPool == 0) { g_refCountPool = new MemoryPool(sizeof(int)); + assert(g_refCountPool); + } _extern._refCount = (int *)g_refCountPool->allocChunk(); *_extern._refCount = 2; @@ -214,7 +216,7 @@ void String::decRefCount(int *oldRefCount) { assert(g_refCountPool); g_refCountPool->freeChunk(oldRefCount); } - free(_str); + delete _str; // Even though _str points to a freed memory block now, // we do not change its value, because any code that calls |