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_Lengthis set to 0.m_Maximumis set tom_Length + 1(1).m_Datais created with a length ofm_Maximum(1).m_Datais cleared and filled with""._AppendSingleis set toStringBase<char>::_AppendDynSingle.- The overloaded operator
StringBase<char>::operator =is called with string"Hello World!" _AppendSingleis called.m_Lengthis 0,m_Maximumis 1.checklenis set tom_Length + src_len + 1(13).m_Maximumis multiplied by 2 until it is larger thanchecklen(16).- The
StringBase<char>::Resizefunction 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.
This is going to be a long one.
First, I decided to check my sanity. Does the CRT memory debugger work correctly?
This correctly reports a leak of 40 bytes. This line fixes the leak:
Okay, what else? Well, maybe the deconstructor is not being called. Let’s put it in a function:
It works, but it leaks. Let’s make absolutely sure the deconstructor is called.
Still leaking. Well, what does the
=operator do? It clears and it appends. Let’s do it manually:Same result, so it has nothing to do with the operator. I wonder what would happen if I removed the
Clear…Alright, it… wait, what? It doesn’t leak? What does
Cleardo then?That’s… harmless. But let’s comment it out.
Same result, no leaks. Let’s remove the call to
Clearagain.Leaking bytes again…
But wait a second, it’s still clearing the
tb::String? The length is set to 0 and the data is zeroed out, even though the body is commented out. How, what…Alright, compiler, let’s see you compile this:
Ha! That will show him! Oh wait… it… still compiles and runs.
Am I using a different version of the same file? No, I only have one version of
TBString2.handTBString2.inlon this computer…Oh.
Oh wait a second.
Oh goddammit.
This better not be what I think it is.
I’m going to murder the person who spent three hours on this.
Oh wait. That was me.
TL;DR: I linked to an old build of the string class, causing all kinds of weird behavior, including leaking memory.