I start with a very simple program:
#include <TBString.h>
int main(int argv, char** argc)
{
tb::String test("");
test = "Hello World!";
return 0;
}
tb::String
is my own string class, which was designed to handle both char
strings and wchar_t
(Unicode) strings. It is heavily templated, tb::String
is a typedef of tb::StringBase<char>
.
The whole thing is compiled using the CRT debugging utilities to check for memory leaks. Here's the output:
Detected memory leaks!
Dumping objects ->
c:\users\sam\svn\dependencies\toolbox\headers\tbstring2.inl(38) : {442} normal block at 0x00D78290, 1 bytes long.
Data: < > 00
{131} normal block at 0x00C5EFA0, 52 bytes long.
Data: < > A0 EF C5 00 A0 EF C5 00 A0 EF C5 00 CD CD CD CD
Object dump complete.
Detected memory leaks!
Dumping objects ->
c:\users\sam\svn\dependencies\toolbox\headers\tbstring2.inl(38) : {442} normal block at 0x00D78290, 1 bytes long.
Data: < > 00
Object dump complete.
The program '[2888] SAM_release.exe: Native' has exited with code 0 (0x0).
So it looks like an empty tb::String (with size 0) is causing the memory leak. Confirmed with this program, which doesn't leak:
#include <TBString.h>
int main(int argv, char** argc)
{
tb::String test("Hello World!");
return 0;
}
Call stack for the original program:
- Create a
StringBase<char>
with string""
. m_Length
is set to 0.m_Maximum
is set tom_Length + 1
(1).m_Data
is created with a length ofm_Maximum
(1).m_Data
is cleared and filled with""
._AppendSingle
is set toStringBase<char>::_AppendDynSingle
.- The overloaded operator
StringBase<char>::operator =
is called with string"Hello World!"
_AppendSingle
is called.m_Length
is 0,m_Maximum
is 1.checklen
is set tom_Length + src_len + 1
(13).m_Maximum
is multiplied by 2 until it is larger thanchecklen
(16).- The
StringBase<char>::Resize
function is called with the new maximum.
Resize function:
template <typename C>
TB_INLINE StringBase<C>& StringBase<C>::Resize(int a_Maximum /*= -1*/)
{
if (!m_Data)
{
m_Maximum = (a_Maximum == -1) ? 4 : a_Maximum;
m_Data = new C[m_Maximum];
StringHelper::Clear<C>(m_Data, m_Maximum);
}
else
{
int newmax = (a_Maximum == -1) ? (m_Maximum * 2) : a_Maximum;
C* temp = new C[newmax];
StringHelper::Clear<C>(temp, newmax);
if (m_Length > 0) { StringHelper::Copy(temp, m_Data, m_Length); }
delete [] m_Data;
m_Data = temp;
m_Maximum = newmax;
}
return *this;
}
This is what I suspect is the problem. Now, my question becomes:
How can I reallocate memory in C++ without it triggering a memory leak in the CRT debugger?
Constructor:
TB_INLINE StringBase<char>::StringBase(const char* a_String)
{
m_Length = StringHelper::GetLength<char>(a_String);
m_Maximum = m_Length + 1;
m_Data = new char[m_Maximum];
StringHelper::Clear<char>(m_Data, m_Maximum);
StringHelper::Copy<char, char>(m_Data, a_String, m_Length);
_AppendSingle = &StringBase<char>::_AppendDynSingle;
_AppendDouble = &StringBase<char>::_AppendDynDouble;
}
Destructor:
TB_INLINE StringBase<char>::~StringBase()
{
if (m_Data) { delete [] m_Data; }
}
Assignment operator:
TB_INLINE StringBase<char>& StringBase<char>::operator = (const char *a_String)
{
Clear();
return (this->*_AppendSingle)(a_String);
}
Append function:
template<>
TB_INLINE StringBase<char>& StringBase<char>::_AppendDynSingle(const char* a_String)
{
if (!a_String) { return *this; }
int src_len = StringHelper::GetLength<char>(a_String);
// check size
if (m_Maximum == -1)
{
m_Maximum = src_len + 1;
m_Data = new char[m_Maximum];
StringHelper::Clear<char>(m_Data, m_Maximum);
m_Length = 0;
}
int checklen = m_Length + src_len + 1;
if (checklen > m_Maximum)
{
while (checklen > m_Maximum) { m_Maximum *= 2; }
Resize(m_Maximum);
}
// append
strcat(m_Data, a_String);
// new length
m_Length += src_len;
return *this;
}
Please note: I do not want to use std::string
or std::vector
, I want to fix this function.