473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

modifying new[]'d objects

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

Jul 23 '05 #1
6 1905
Tim Conkling wrote:
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)
"Of little consequence"? Are you sure? No dynamic objects?
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
Sounds like bad dynamic memory management. Check this out:

class Bad {
char *str;
int a;
public:
Bad(const char* s) : str(new char[strlen(s)+1]) {
strcpy(str, s);
}
~Bad() { delete[] str; }
};

class HasBad {
Bad bad;
public:
HasBad() : bad("badbadbad") {}
HasBad(const HasBad& hb) : bad(hb.bad) {}
};

Now copy a 'HasBad' object or pass it into a function by value, and you
going to have all kinds of problems. Is "HasBad" the culprit? No. It's
the 'Bad'. So, in what you've shown 'TitleAttributes' is probably not
the class itself, but one of those things you think are "of little
consequence".
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.


What you showed is OK. However, there is always that thing, you know,
that you didn't show. There is always doubt about it, you see...

Also, _sometimes_ (really rarely) your implementation of the standard
classes could be buggy. You could try switching to another library
implementation, but only when you exhaust all possibilities to fix your
own code.

V
Jul 23 '05 #2
On 2005-06-30 15:32:22 -0400, Victor Bazarov <v.********@comAcast.net> said:
Tim Conkling wrote:
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)


"Of little consequence"? Are you sure? No dynamic objects?
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


Sounds like bad dynamic memory management. Check this out:

class Bad {
char *str;
int a;
public:
Bad(const char* s) : str(new char[strlen(s)+1]) {
strcpy(str, s);
}
~Bad() { delete[] str; }
};

class HasBad {
Bad bad;
public:
HasBad() : bad("badbadbad") {}
HasBad(const HasBad& hb) : bad(hb.bad) {}
};

Now copy a 'HasBad' object or pass it into a function by value, and you
going to have all kinds of problems. Is "HasBad" the culprit? No. It's
the 'Bad'. So, in what you've shown 'TitleAttributes' is probably not
the class itself, but one of those things you think are "of little
consequence".
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.


What you showed is OK. However, there is always that thing, you know,
that you didn't show. There is always doubt about it, you see...

Also, _sometimes_ (really rarely) your implementation of the standard
classes could be buggy. You could try switching to another library
implementation, but only when you exhaust all possibilities to fix your
own code.

V


All the other member variables are basic built-in datatypes. There are
no complex objects and nothing is dynamically allocated. The class
looks exactly like this:

class TileAttributes
{
public:

enum CollisionType
{
kNoSides = 0,
kTopSide,
kAllSides
};

float friction;
CollisionType collisionType;
bool climbable;
unsigned short animationLength;
unsigned short animationPause;
short collisionClassLowerBound;
short collisionClassUpperBound;
std::string collisionEventName;
};

Jul 23 '05 #3
Tim Conkling wrote:
[...]
All the other member variables are basic built-in datatypes. There are
no complex objects and nothing is dynamically allocated. The class looks
exactly like this:
No, it can't be. In the previous post there were two constructors,
a virtual destructor, and an assignment operator. Where did they go?
Also, there was a member called 'm_eventName'. Where is it?

So, you're not showing all the code again. What do you want us to do,
read your mind or the contents of your computer memory remotely?

class TileAttributes
{
public:

enum CollisionType
{
kNoSides = 0,
kTopSide,
kAllSides
};

float friction;
CollisionType collisionType;
bool climbable;
unsigned short animationLength;
unsigned short animationPause;
short collisionClassLowerBound;
short collisionClassUpperBound;
std::string collisionEventName;
};


OK, fine. The data part probably looks like that. Please, don't take my
suggestions too literally. I said that the problem was in the code you
didn't show. Was I wrong?

Here is how I arrived to the "not here" conclusion. You said there was
a problem. You showed some code. I looked at the code shown. There was
no problem with that code. Therefore, the problem _must_ be elsewhere.
Am I being unreasonable here?

You said the error you got was "double free". That's some bad memory
management (do you agree?). It could be due to some bad memory management
in one of subobjects of TitleAttributes. Or it could be due to some bad
memory management in some other place. It still could be the 'eventName'
(or whatever you decide to call it), although I strongly doubt that. You
should check the implementation of 'std::string' though. Try an older
version of the same compiler (with its older version of the standard
classes). Or try another compiler (if you can).

V
Jul 23 '05 #4
I appreciate the help and I'm sorry for the confusion. What I meant to
say is that the _data_ portion of the class looks exactly like what I
copied-and-pasted in the previous message. I didn't include the member
functions because I had already shown what they generally looked like.

In my original message, I was attempting to save space, and I wrote
the class definition from memory. It didn't come out
character-for-character the way it appears in my header file. The point
is, there is only one member variable of note and its type is
std::string. All the other variables are built-in datatypes.

I seriously doubt the problem is with the std::string implementation,
so I'll look at other parts of the code.

Jul 23 '05 #5
Tim Conkling wrote:
[snip]
TileAttributes& operator= (const TileAttributes& copy) {
m_eventName.assign(copy.m_eventName); }
};


Presumably in reality this assignment operator returns something?

James
Jul 23 '05 #6
On 2005-07-01 05:53:31 -0400, James Lothian
<ja***@jameslothian.freeserve.co.uk> said:
Tim Conkling wrote:
[snip]
TileAttributes& operator= (const TileAttributes& copy) {
m_eventName.assign(copy.m_eventName); }
};


Presumably in reality this assignment operator returns something?

James


Yes, it does... and I have learned my lesson about typing code from
memory. It's a bad thing, and causes all sorts of confusion when real
code is mixed in with code-off-the-top-of-my-head.

FWIW, the problem has been corrected. It was bad memory management on
my part. I was memcpy'ing an array of these objects to another array,
which worked fine back when all the objects data members were simple
data types, and obviously broke when I added the std::string member.
Stupid...

Sorry for the confusion, Victor and James.

Tail between my legs,
Tim

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Japcuh | last post by:
How do you write self modifying code in Java? Japcuh (Just Another Perl C Unix Hacker) http://www.catb.org/~esr/faq/hacker-howto.htm#what_is ..0. ...0 000
6
by: Peter Ballard | last post by:
Whew. I hope that title is descriptive! Hi all, The python tutorial tells me "It is not safe to modify the sequence being iterated over in the loop". But what if my list elements are mutable,...
0
by: Gouda Man | last post by:
Hey everyone, I'm embedding the Python interpreter within my program to use as a scripting language. However, one of the unique features of our program is the ability of the internal language to...
6
by: qwweeeit | last post by:
Hi all, when I was young I programmed in an interpreted language that allowed to modify itself. Also Python can (writing and running a module, in-line): fNew =open("newModule.py",'w') lNew=...
12
by: pvinodhkumar | last post by:
1) char* p = "Plato"; p = 'r'; // runtime error 2) char c = "Plato"; c = 'i';// ok.Why no runtime here?Why is the contradiction? cout << c << endl;
7
by: Jan Gregor | last post by:
Hello folks I want to apply changes in my source code without stopping jython and JVM. Preferable are modifications directly to instances of classes. My application is a desktop app using swing...
9
by: John | last post by:
If a value type is immutable, I guess it's threadsafe to read it? But not threadsafe to assign a new value to it (can any value type be truely immutable? Isn't assigning a totally new value to it,...
6
by: MackS | last post by:
Hello everyone I am faced with the following problem. For the first time I've asked myself "might this actually be easier to code in C rather than in python?", and I am not looking at device...
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.