By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Class Member Getting Corrupted

P: n/a
I have the following situation:

class Y;
class Z;

class X {
public:
Y y;
Z* z;
};

X::X() : y(3) {
z = new Z;
};

class Z is fairly complicated, i.e., it publicly inherits from two base
classes, one of those base classes itself publicly inherits from two base
classes, etc.

When X::X is in the assignment phase, mmebers in y are getting corrupted
during the construction of z.

Are there any typical problems to look for to solve this mess? Using g++,
are there any compiler flags that should be turned on that may point to the
problem area or assist with debugging using gdb?
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Paul Jackson wrote:
I have the following situation:

class Y;
class Z;

class X {
public:
Y y;
Z* z;
};

X::X() : y(3) {
z = new Z;
};

class Z is fairly complicated, i.e., it publicly inherits from two base
classes, one of those base classes itself publicly inherits from two base
classes, etc.

When X::X is in the assignment phase, mmebers in y are getting corrupted
during the construction of z.

Are there any typical problems to look for to solve this mess? Using g++,
are there any compiler flags that should be turned on that may point to the problem area or assist with debugging using gdb?


Chop Z in half (that means comment out half its members, then force it to at
least compile if not behave). The problem might change, or go away.

When the problem goes away, you have chopped the problem out of Z. Now
activate all the code you commented out, and vice versa. Keep chopping
(essentially a binary search algorithm) until you have only a few variables
left.

Then write lots of automated tests on your code so this kind of thing likely
won't happen again.

--
Phlip
Jul 22 '05 #2

P: n/a
Original post corrected below.
"Paul Jackson" <pa**********@jhuapl.edu> wrote in message
news:br**********@houston.jhuapl.edu...
I have the following situation:

class Y;
class Z;

class X {
public:
Y y;
Z* z;
};

X::X() : y(3) {
z = new Z;
};

class Z is fairly complicated, i.e., it publicly inherits from two base
classes, one of those base classes itself publicly inherits from two base
classes, etc.

When one of the base classes of Z is in the assignment phase of its constructor, members in y are getting corrupted.

Are there any typical problems to look for to solve this mess? Using g++,
are there any compiler flags that should be turned on that may point to the problem area or assist with debugging using gdb?

Jul 22 '05 #3

P: n/a
"Paul Jackson" <pa**********@jhuapl.edu> wrote...
Original post corrected below.
"Paul Jackson" <pa**********@jhuapl.edu> wrote in message
news:br**********@houston.jhuapl.edu...
I have the following situation:

class Y;
class Z;
Forward-declarations?

class X {
public:
Y y;
Y is undefined, the compiler is unlikely to let you do that.
Z* z;
};

X::X() : y(3) {
z = new Z;
};
Extraneous semicolon...

So, the code you posted is not going to compile. What do you
really expect from us? A recommendation on how to fix the code
that you haven't even posted?

class Z is fairly complicated, i.e., it publicly inherits from two base
classes, one of those base classes itself publicly inherits from two base classes, etc.
What's in 'etc.'? Post it all, otherwise why post at all?

When one of the base classes of Z is in the assignment phase of its constructor,
members in y are getting corrupted.
Memory overrun, most likely. Read the FAQ 5.8.

Are there any typical problems to look for to solve this mess?


Here is something that might be similar (the code has undefined
behaviour, but typically memory overrun occurs and the 'y' member
is corrupted):

#include <stdlib.h>

class A {
char x[7];
int y;
public:
A(const char* str) : y(3) {
strcpy(x, str); // undefined behaviour if 'strlen(str)'
// is greater than 6
}
};

int main() {
A a("abcdefghij");
}

As you can see there are only 7 chars allocated for 'x' and 10
characters (plus the null terminator) are used to initialise it.

'strcpy' copies "hij\0" into the member 'y' (most likely). Of
course, it's not defined by the language, but is one of the expected
results.
Using g++,
are there any compiler flags that should be turned on that may point to

the
problem area or assist with debugging using gdb?


Particular compiler flags or particular techniques using particular
products should be discussed in their respective newsgroups. Try
gnu.g++.help.

Victor
Jul 22 '05 #4

P: n/a
The following article might help in debugging:

http://www.eventhelix.com/RealtimeMa..._crashes_2.htm

The article focuses on C++ debugging.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.