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 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
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;
};
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
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.
Tim Conkling wrote: [snip] TileAttributes& operator= (const TileAttributes& copy) { m_eventName.assign(copy.m_eventName); } };
Presumably in reality this assignment operator returns something?
James
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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,...
|
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...
|
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=...
|
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;
|
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...
|
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,...
|
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...
|
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....
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |