I am observing a std::string
assignment operator (=
) causing an access violation writing to the LHS. In MSVC++ debug mode, the LHS internal buffer points to an invalid address. I'm not familiar with the internals of MSVC++ std::string
but I had previously assumed that the internal buffer pointer should never be invalid.
Using the Visual Studio debugger, the internal buffer to which I refer is the char[]
instance member std::string::_Bx::_Buf
. This usually holds the address of the null-terminated character string represented by the std::string
object. It appears that std::string::_Bx._Ptr
is also a char *
pointer to this address.
I am encountering this frequently in certain circumstances but I can't determine how or when this address becomes invalid. Wouldn't the debugger alert me if something clobbered this value? Is there a way to set the Visual Studio debugger to pause when std::string::_Bx::_Buf
is accessed for writing?
This is a scenario in which I cannot provide a SSCCE because I can't intentionally duplicate the error. The code that invokes the error is just a typical string value assignment in an instance mutator, like:
class MyClass {
protected:
std::string myValue;
public:
void setValue(std::string value) {
myValue = value; // ACCESS VIOLATION from std::string::operator=()
}
};
class OtherClass {
static myFunc() {
std::string myString("some value");
MyClass *myClass = new MyClass();
myClass->setValue(myString); // ACCESS VIOLATION from setValue()
}
};
What could cause this? Has anyone seen this before? Any suggestions on where to look next?