const_string lib issue
I'm trying const_string lib that looks not bad, but it crashes at runtime with access violation(atomic_count, operator++()). The test code:
#include <boost/const_string/const_string.hpp>
#include <boost/const_string/concatenation.hpp>
typedef boost::const_string<wchar_t> wcstring;
class Test
{
private:
const wcstring &s1;
const wcstring &s2;
public:
Test()
: s1(L"")
, s2(L"")
{
}
const wcstring &GetS1()
{
return s1;
}
const wcstring &GetS2()
{
return s2;
}
};
Test t;
int _tmain(int argc, _TCHAR* argv[])
{
//Test t;
wcstring t1 = t.GetS1(); // crashes here
wcstring t2 = t.GetS2();
return 0;
开发者_如何学C}
It crashes only if t is global. If I move declaration into main(), it's ok. System: VS 2010, boost v. 1.47.0
The question: Am I doing something wrong or is it problem of library / compiler? Can someone recommend a more stable implementation of immutable strings for C++?
Your instance of Test
has initialized its reference data members as references to temporaries created from the literals L""
.
Oops. The temporaries no longer exist by the time you try to use one of them in the copy constructor of wcstring
at the line that crashes, so your references don't refer to anything.
I think boost::const_string
should pretty much always be used by value, that's what it's for.
精彩评论