Nmx wrote:
// Dirty hack to avoid non-threadsafeness of string class
// We set ref to big value here so it will not reach 0
// Works only with GNU libstdc++ STL!
static long* ref = (long*)config_name.data() - 2;
*ref = 0x40000000;
--
I don't have a clue of this do except for a really bad side effect: if
I change the value of any std::string initialized as an empty string
then every string initialized by the same way has his value modified.
If I strip out this piece of code then the side effect disappear.
Does anyone have a clue of what this code really do?
Nobody answered in an hour, so I will provide the generic answer:
Take it out, and carefully review the rest of the code. Someone capable of
commenting such a diseased piece of code as a mild "hack" is capable of
writing anything and not commenting it.
The hack appears to reach into the secret shared string that's inside many
std::string representations. It sets the reference count to a huge number,
allowing the referred-to string data to leak. That, in turn, lets other
threads read the leaked data after its owning thread has destroyed it.
I don't know why this would affect other strings. The hack might have once
worked "better" than it does now. Your compiler, your input libraries, even
your linker settings may have changed since the hack worked. The symptom you
report might change if you compile a different way.
You need to perform the tiny bit of research that your predecessor deferred.
Find a forum that discusses the thread package this program uses, and ask it
how to pass a string between two threads. They will help you write something
simple and robust.
Whatever the problems of this hack, you might get problems if you take it
out. The other thread might access the string after this thread destroys it.
In general, you must maintain this code's value without increasing your
burden of debugging it. Read /Working Effectively with Legacy Code/ by Mike
Feathers to learn to do that.
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!