I have a class, C. C has a member variable declared as: bool markerStart;
From within C a call to sizeof(*this) gives a value of 0x216 bytes.
Elsewhere within C, I do: markerStart = false;
Rather than setting markerStart to false, this call is actually clobbering the start of the next class in memory!
Looking at the disassembled code, I find:
markerStart = false;
06FB6B7F mov eax, dword ptr [this]
06FB6B78 mov byte ptr [eax+218h], 0
The second move instruction is setting a byte at this + 0x218 to zero, but since the class is only 0x216 bytes long, this is clobbering memory!
In response to a comment, it definitely is the markerStart = false instruction. I can watch it happening in the disassembler view and in the memory view (and using Windbg, by using a data breakpoint). The first byte of the next class gets set to zero, which messes up its vftbl pointer.
Note: taking the address of markerStart and subtracting it from this, yields 0x211!
Can anyone give me a clue on where to start looking to resolve this problem?
Update: Thanks for all the help. Without code, it was next to impossible for any of you to solve the problem. What I was looking for were hints as to where to start looking. Most of you provided excellent hints, so thank you!
I finally found the problem. In this case alignment had been set in one class, and not been correctly reset following the critical block of code. The class with the faulty alignment happened to get compiled immediately before the declaration of class C - hence that's where the problem showed up.
You need to post more code -- even better would be if you could prune it down to the minimum class definition where your anomaly occurs. That in itself will probably help you identify what's happening.
Some possibilities that occur to me:
Without more information, I'd go for the ODR violation. Those can be insidious, and impossible to detect when compiling or linking.
As pointed out by Pontus, you probably broken the one definition rule somehow. You've probably included the header file with the definition of class C in two translation units where other code, often in a preceding header file, changes the way the definition of class C is interpreted so that it has a different size in the two translation units.
One possibility, is that you've accidently changed the default member alignment (either through a command line argument to the compiler or via a #pragma in the source) so that two different translation units think the structure has a different size because of differing amounts of padding (most x86 compilers default to alignment on 4-byte boundaries, but allow you to request alignment on 1-byte boundaries as needed). Look in the other header files for a #pragma changing the default alignment that is missing a following #prama to restore it back to the previous value (you don't specify what compiler, so I can't give specifics).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With