c++buildervclc++builder-6

Thread safety of AnsiString reference counting mechanism


My question is about AnsiString in Borland C++Builder 6.0
As you know, VCL's AnsiString uses reference counting.
Is that reference counting thread safe?
For example, suppose we have std::queue<AnsiString> asq;
We push an AnsiString value in that queue in thread#1, and then we do asq.pop() in thread#2. (The access to asq itself is synchronized.) Strings are not modified.
Is it possible I will have a memory leak when AnsiString's destructor is called when using AnsiString(s) that way?


Solution

  • Yes, the refcount is thread-safe. The RTL places a lock on the refcount whenever it is being incremented and decremented. There will not be any memory leak in the scenario you describe.