Class Member Getting Corrupted

Discussion in 'C++' started by Paul Jackson, Dec 19, 2003.

  1. Paul Jackson

    Paul Jackson Guest

    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?
     
    Paul Jackson, Dec 19, 2003
    #1
    1. Advertising

  2. Paul Jackson

    Phlip Guest

    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
     
    Phlip, Dec 19, 2003
    #2
    1. Advertising

  3. Paul Jackson

    Paul Jackson Guest

    Original post corrected below.


    "Paul Jackson" <> wrote in message
    news:brvsto$3vt$...
    > 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?
    >
    >
     
    Paul Jackson, Dec 22, 2003
    #3
  4. "Paul Jackson" <> wrote...
    > Original post corrected below.
    >
    >
    > "Paul Jackson" <> wrote in message
    > news:brvsto$3vt$...
    > > 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
     
    Victor Bazarov, Dec 22, 2003
    #4
  5. EventHelix.com, Dec 22, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TS

    viewstate getting corrupted

    TS, Dec 2, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    436
    Steven Cheng[MSFT]
    Dec 5, 2005
  2. E11
    Replies:
    1
    Views:
    4,942
    Thomas Weidenfeller
    Oct 12, 2005
  3. Siemel Naran
    Replies:
    4
    Views:
    838
    Micah Cowan
    Jan 12, 2005
  4. Enter The
    Replies:
    0
    Views:
    515
    Enter The
    Oct 19, 2007
  5. Rohith KS
    Replies:
    1
    Views:
    130
Loading...

Share This Page