Fixed reported memory leak in debug build.

This commit is contained in:
Couriersud 2014-01-21 23:34:49 +00:00
parent 92f475be80
commit 38d43fc7fd
2 changed files with 13 additions and 11 deletions

View File

@ -14,7 +14,7 @@
pblockpool pstring::m_pool; pblockpool pstring::m_pool;
pstring::str_t *pstring::m_zero = NULL; pstring::str_t pstring::m_zero;
/* /*
* Uncomment the following to override defaults * Uncomment the following to override defaults
@ -155,7 +155,7 @@ pstring pstring::vprintf(va_list args) const
void pstring::sfree(str_t *s) void pstring::sfree(str_t *s)
{ {
s->m_ref_count--; s->m_ref_count--;
if (s->m_ref_count == 0) if (s->m_ref_count == 0 && s != &m_zero)
m_pool.dealloc(s); m_pool.dealloc(s);
} }
@ -178,9 +178,6 @@ pstring pstring::sprintf(const char *format, ...)
void pstring::resetmem() void pstring::resetmem()
{ {
// Release the 0 string // Release the 0 string
if (m_zero != NULL)
sfree(m_zero);
m_zero = NULL;
m_pool.m_shutdown = true; m_pool.m_shutdown = true;
m_pool.resetmem(); m_pool.resetmem();
} }

View File

@ -97,6 +97,7 @@ public:
inline const char *cstr() const { return m_ptr->str(); } inline const char *cstr() const { return m_ptr->str(); }
// concatenation operators // concatenation operators
pstring& operator+=(const char c) { char buf[2] = { c, 0 }; pcat(buf); return *this; }
pstring& operator+=(const pstring &string) { pcat(string.cstr()); return *this; } pstring& operator+=(const pstring &string) { pcat(string.cstr()); return *this; }
friend pstring operator+(const pstring &lhs, const pstring &rhs) { return pstring(lhs) += rhs; } friend pstring operator+(const pstring &lhs, const pstring &rhs) { return pstring(lhs) += rhs; }
friend pstring operator+(const pstring &lhs, const char *rhs) { return pstring(lhs) += rhs; } friend pstring operator+(const pstring &lhs, const char *rhs) { return pstring(lhs) += rhs; }
@ -132,6 +133,13 @@ public:
return (result != NULL) ? (result - cstr()) : -1; return (result != NULL) ? (result - cstr()) : -1;
} }
inline int find(const char search, int start = 0) const
{
int alen = len();
const char *result = strchr(cstr() + MIN(start, alen), search);
return (result != NULL) ? (result - cstr()) : -1;
}
// various // various
inline bool startsWith(const pstring &arg) const { return (pcmp(cstr(), arg.cstr(), arg.len()) == 0); } inline bool startsWith(const pstring &arg) const { return (pcmp(cstr(), arg.cstr(), arg.len()) == 0); }
@ -166,6 +174,7 @@ protected:
struct str_t struct str_t
{ {
str_t() : m_ref_count(1), m_len(0) { m_str[0] = 0; }
str_t(int alen) : m_ref_count(1), m_len(alen) { m_str[0] = 0; } str_t(int alen) : m_ref_count(1), m_len(alen) { m_str[0] = 0; }
char *str() { return &m_str[0]; } char *str() { return &m_str[0]; }
@ -183,11 +192,7 @@ protected:
private: private:
inline void init() inline void init()
{ {
if (m_zero == NULL) m_ptr = &m_zero;
{
m_zero = new(pstring::m_pool, 0) pstring::str_t(0);
}
m_ptr = m_zero;
m_ptr->m_ref_count++; m_ptr->m_ref_count++;
} }
@ -225,7 +230,7 @@ private:
static str_t *salloc(int n); static str_t *salloc(int n);
static void sfree(str_t *s); static void sfree(str_t *s);
static str_t *m_zero; static str_t m_zero;
}; };