The following snippet compiles and runs perfectly in all configurations, except when I strip
the debug symbols, which makes it crash with a double free of the internal string pointer when t
goes out of scope in bar()
. malloc: *** error for object 0x11af79760: pointer being freed was not allocated
I can't find anything in any example, or documentation that suggests this is the wrong way to use ostringstream
and ss.str()
.
std::wstring foo() {
std::wostringstream ss;
ss << L"this is a not too long string";
return ss.str();
}
void bar() {
// unrelated stuff
auto t = foo();
// unrelated stuff
}
This is on iOS, using Xcode 7.2, w. clang++ 7.0.2 (700.1.81), c++14 enabled and regardless of optimization level (assuming it's not completely optimized away).
This is in a rather large project, I've not been able to reproduce it in a minimal project so far. I can move the code around, and it still happens every time, which to me eliminates memory corruption. (Code also looks fine) The complexity of what's written to ss
doesn't matter, I can even remove that line.
I can't get the same code to break on Mac using the same compiler and code, suggesting that perhaps the problem is related to arm in combination with symbol stripping, than the c++ implementation.
I haven't read up enough on the internals of what the symbol stripping does, apart from what I expect it to do, but it seems very odd to me that stripping symbols would affect anything.
I've tried all sorts of reasonable and unreasonable ways of wrapping the return with an extra string, and to manually access the internal rdbuf, but it doesn't matter (the library implementation does exactly that anyway).
The app immediately crashes when this happens, so instrumenting with Zombies hasn't provided anything useful so far. I've also tried compiling and running on multiple devices, with clean builds, and incremental.
I'm also terribly rusty when it comes to debugging C++ arm assembler (especially in Xcode), so I've not been able to locate anything odd there.