I've encountered a weird problem. I'm making a DLL to store some classes (with abstract interfaces). Each of the classes have a std::string ErrorMessage member and a std::string GetErrorMessage() method to keep track on possible errors.
Now every time I store the value from GetErrorMessage() in a std::string in my main prog (not the DLL) the program crashes after FreeLibrary() is called. However it only occurs if the string hasn't been destroyed yet.
My guess would be that when the string is returned it's not actually copied but pointing to the original string in the DLL class, so when FreeLibrary is called it points to invalid memory.
I also noticed if I do a string operation on the string, like:
It doesn't crash. Since the string is modified it gets its own memory and no longer points to the one inside the DLL.Code:std::string SomeError = Class->GetErrorMessage(); SomeError += "XXX";
I don't know of the inner workings of std::string, but this seemed reasonable considering the symptoms. So, my questin is is there an elegant solution to this? I tried both SomeError += "" and SomeError = std::string(Class->GetErrorMessage()) to explicitly make a copy but without altering the contents, but both failed (guess the compiler outsmarted me :/ ). Ideas?