c_str() does not return a string
but a char const*
. The buffer it points to is only valid during the lifetime of the std::string
and even may be invalidated by subsequent operations on the string (s.a. docs). Knowing this I would consider it a rather expected behaviour to get access violations when trying to access this buffer after the string is deleted. That this is working on other platforms because the memory usually is not overwritten a few cycles later does not neither mean it is guaranteed to nor that it should.
To work around this just keep the string as long as you need it. This really shouldn’t mess up your memory footprint, if it does there probably is another issue elsewhere.