Sign in to follow this  
Red Ant

[C++] Memory corruption - member of derived class overlaps with member of base class!

Recommended Posts

Red Ant    471
Hello, I've gone through a lot of stupid mistakes in my coding career, but I've never seen this one before: I have a base class, which, for simplicity's sake, we'll just call Base. Not surprisingly, Derived is derived from Base. Base has a bunch of member variables and Derived adds some more. So far so good, right? Now at some point my program just segfaults. At first I was completely puzzled as to what caused the crash. For 2 days I kept stumbling completely in the dark. Then I noticed that the crash only happened when I had previously made an assignment to Base's member m_pExceptionResult (which is a boost::shared_ptr). I started looking for possible out-of-bound array accesses and other such things that tend to corrupt a program's memory but couldn't find any place I did anything like that. So I took a look at the memory addresses of the members of Base and Derived respectively, and this is what I found out: - m_pExceptionResult (boost::shared_ptr), the last member of Base, starts at address 0x1857268 ... sizeof( pExceptionResult ) gives 8 bytes - m_value (an std::basic_string< wchar_t >), the first member of Derived, starts at address 0x1857264 ... sizeof( m_value ) gives 32 bytes As you can see from that, the memory occupied by m_pExceptionResult partially overlaps with that of m_value!! So my question to that amounts to a simple WTF??? P.S. I'm using Visual C++ 2005. P.S. #2: Base is also derived from boost::noncopyable.

Share this post


Link to post
Share on other sites
Schrompf    1035
Last time I encountered this was on Visual C++ 6. Members of a derived class overlapped members of the base class just like you described. A simple Rebuild All fixed the case, and it never returned.

Share this post


Link to post
Share on other sites
Bregma    9202
You've definitely described the symptom of a fragile base class causing ABI incompatibility between modules (the cure as mentioned is a complete clean-and-rebuild).

Either your build tools are inadequate (They should detect such a situation) or you are missing some vital dependency information, like required header files are not being included where they should be. This problem crops up most often when linking in external dependencies.

If you work on a larger project with a team of size > 1 you tend to see this more often.

Share this post


Link to post
Share on other sites
Red Ant    471
Hi guys! The problem was .... struct member alignment!! The classes I've mentioned live in a static library (uses default struct member alignment), which is linked against my application project (uses struct member alignment of 1 byte).

What I didn't mention was that Derived is a template, while Base is not. So due to differing alignment settings, the compilation units in my application project had a different idea of the memory layout of Base than it actually had, which needless to say, led to PROBLEMS. :P

Now I'm faced with the task of isolating the part of my project that really NEEDS that stupid 1 byte alignment setting from the rest that doesn't (1 byte alignment necessary because the project includes some driver code for a network card ... not written by me).

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Now I'm faced with the task of isolating the part of my project that really NEEDS that stupid 1 byte alignment setting from the rest that doesn't (1 byte alignment necessary because the project includes some driver code for a network card ... not written by me).


Since you are using VC++: That is what pushing/popping of pragmas is for. Make sure the alignment pragma is popped after the declaration of the must-be-aligned struct, so that the compiler doesn't try to (inconsistently) align others.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this