T
Tim Conkling
I'm having difficulty tracking down a bug in my program and I'm
wondering if it's due to some misuse on my part of new[]'d objects.
Here's the situation. If anyone can shed some light on this, I'd be
much obliged.
I have a simple class that looks something like this (this is a rather
abbreviated version):
class TileAttributes
{
... (a few other member variables of little consequence here)
std::string m_eventName;
TileAttributes() : m_eventName("") {}
TileAttributes(const TileAttributes& copy) : m_eventName(copy.m_eventName) {}
virtual ~TileAttributes() {}
TileAttributes& operator= (const TileAttributes& copy) {
m_eventName.assign(copy.m_eventName); }
};
I have two arrays of TileAttributes, created with the new[] operator,
and I do a lot of this sort of thing:
array1[x] = array2[y];
All the member variables of TileAttributes are integral types, except
for m_eventName. I just added the m_eventName member the other day, and
since then I've been getting crashes when modifying these arrays. I am
using gcc 4 and gdb 6.1 (via Apple Xcode) and I'm getting errors that
look like this:
malloc: *** error for object 0x166a0210: double free
These errors (usually a few of them) always precipitate a crash in the
operator=() function of TileAttributes, specifically the line where I
am doing:
m_eventName.assign(copy.m_eventName)
The callstack as it exists above my operator= function looks like this:
__cxa_throw
std::__throw_length_error
std::string::_Rep::_S_create
std::string::_Rep::_M_clone
std::string::assign
This problem only occurs when m_eventName.length() > 0. I didn't even
have a deep copy constructor or assignment operator before I added the
m_eventName member, but I added them to see if they might solve my
problem. They made it easier to pinpoint exactly where the error was
occuring, but I am still baffled. I can't see what I might be doing
wrong here.
Tim
wondering if it's due to some misuse on my part of new[]'d objects.
Here's the situation. If anyone can shed some light on this, I'd be
much obliged.
I have a simple class that looks something like this (this is a rather
abbreviated version):
class TileAttributes
{
... (a few other member variables of little consequence here)
std::string m_eventName;
TileAttributes() : m_eventName("") {}
TileAttributes(const TileAttributes& copy) : m_eventName(copy.m_eventName) {}
virtual ~TileAttributes() {}
TileAttributes& operator= (const TileAttributes& copy) {
m_eventName.assign(copy.m_eventName); }
};
I have two arrays of TileAttributes, created with the new[] operator,
and I do a lot of this sort of thing:
array1[x] = array2[y];
All the member variables of TileAttributes are integral types, except
for m_eventName. I just added the m_eventName member the other day, and
since then I've been getting crashes when modifying these arrays. I am
using gcc 4 and gdb 6.1 (via Apple Xcode) and I'm getting errors that
look like this:
malloc: *** error for object 0x166a0210: double free
These errors (usually a few of them) always precipitate a crash in the
operator=() function of TileAttributes, specifically the line where I
am doing:
m_eventName.assign(copy.m_eventName)
The callstack as it exists above my operator= function looks like this:
__cxa_throw
std::__throw_length_error
std::string::_Rep::_S_create
std::string::_Rep::_M_clone
std::string::assign
This problem only occurs when m_eventName.length() > 0. I didn't even
have a deep copy constructor or assignment operator before I added the
m_eventName member, but I added them to see if they might solve my
problem. They made it easier to pinpoint exactly where the error was
occuring, but I am still baffled. I can't see what I might be doing
wrong here.
Tim