I was hoping someone here could make sense of what I think could be a MSVC 2010 bug.
I have this code (as part of a much larger project):
namespace MyNamespace{
class Foo{
//snip
QPointer<MyNamespace::Baz> :: x;
};
class Baz{
//snip
};
//snip
Foo::Bar(){
x = new MyNamespace::Baz();
ThisFunctionWillSegfaultIfXIsNull(x);
//snip
}
Again, keep in mind, this is part of a much larger project, involving cmake, qt, and a whole host of other exciting things.
I've been tracing through this with a debugger. When I compile this project in Debug mode, everything goes as expected. However, when I compile this as a Release project, x gains the value of 0x0000000000000000.
Also, when tracing through this in the MSVS debugger (still in release mode), a few interesting things happen:
The constructor for Baz is in fact called, and "this" has a valid memory location inside of Baz's constructor.
After returning from the constructor (back in
Foo::Bar()
), the value of this (presumably "Foo") is now THE SAME as the value of "this" inside of Baz's constructor (presumely "Baz").None of Foo's data appears to have been lost, even though it just apparently changed memory addresses.
"This" (as Foo) somehow magically grows a new data member of type MyNamespace::Baz, that contains all of the correct data that was assigned during the constructor.
So apparently compiler optimization is making something extremely mystical occur. Again, this only seems to occur in Release mode, which makes me think that A) the optimizer is making a difference and B) makes it difficult for me to get further information on what is actually happening.
Has anyone seen this before? What is happening here? How can I get my next function to stop segfaulting because X is null?