Whenever I output a particular pointer address to std::cout, I get a crash:
bool MyClass::foo() const
{
std::cout << "this prints fine" << std::endl << std::flush;
std::cout << d << std::endl << std::flush; // crash!
return true;
}
Where d is a pointer member of the class, i.e.:
class MyClass {
// ...
private:
MyClassPrivate* d;
};
What could cause the application to crash? Even if it is a NULL pointer, or an initialized pointer, it should still print out the (perhaps invalid) address, right?
The application is compiled in debug mode, if that makes a difference. The function foo is not marked as inline.
Background: I am trying to track down a bug in an external application process. The bug is only caused when another application sends rapid-fire command to the process. I’m using std::cout to trace the execution of the external process.
If
thisis not a valid pointer, any access to a member field might cause an access violation. Non-virtual methods called on invalid pointers work just fine until they try to access a field, because the call itself doesn’t need to dereferencethis.For instance, this situation would crash roughly as you describe: