473,473 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Class Member Getting Corrupted

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
4 1696
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ding feng | last post by:
I am a beginner. So this question could be very stupid. Would anyone help me to solve this problem? A formatted txt file is read. Then i need to look into a vector who is a member of a class to...
1
by: sajidazmi | last post by:
Hi, I'm coding an Stock Order Entry System. The problem I'm facing is when I'm passing a pointer to one class, the pointer gets corrupted. I'm not able to see why? Here is the code, please tell...
0
by: Mark Sicignano | last post by:
I found the reason for at least one of the script bugs that appear in the Add Member Variable Wizard. This is a followup to a 9-month old article. Dave V said: > An error has occurred in the...
12
by: Saurabh Kumar | last post by:
I have a byte array, which i need to send to a class, where it will be stored in an arraylist. I use the following property in the class to accept the byte array, but it seems that the array is...
11
by: DrNoose | last post by:
Hi! I've got a program that's almost done, but I'm getting compile errors in two lines: 317 & 319. I think the main error has to do with the Truck Class. I'm a newbie and keep looking at the...
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
43
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions...
6
by: VSP | last post by:
Hello, I am just implementing singleton pattern in various ways. In one implementation I created a static member and returning that static member in the getInstance() function. I have made...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.