eastl::shared_ptr<>::reset(): function not thread safe #378
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
reset() function of shared_ptr is not thread safe.
It takes so many times to understand/find where come from that memory leak I have in my code. The problem was so difficult to reproduce and isolate!
So here the code:
If 2 threads, with 2 shared_ptr<> pointed the same value, and are calling the release() function at the same time, the
free_ref_count_sp();
could be not called.It happens, when thread 1 is calling
Internal::atomic_decrement(&mWeakRefCount);
(duem_RefCount > 0
) after the thread 2 call the codeif(Internal::atomic_decrement(&mWeakRefCount) == 0)
. In that casefree_ref_count_sp();
is not called, because on the thread 2,mWeakRefCount
will have value 1.The fix consist to remove that else condition and process each counter independently :
I try to write UTest for that case. For the moment, I used std::future and std::async because it's easier for me. If I have to used EAThread, could you tell me the best eastl function to used instead.