Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSharedPtr thread safety #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mlavik1
Copy link

@mlavik1 mlavik1 commented Jun 30, 2021

The issue:
I have encountered an occasional crash when importing large glTF models at runtime.
After looking into the issue, the crash seemed to be caused by multiple threads incrementing/decrementing the SharedReferenceCount of a TSharedPtr pointing to a FJsonValueArray (usually the "bufferViews" array). This operation is not atomic by default.

The fix:
Since TSharedPtr is not thread-safe by default, we have to change the Mode-parameter of MakeShared when creating the shared pointers. However, since MakeShared is called by Unreal's JsonSerializer (which uses the default Mode) we can't control how each shared pointer is constructed and will instead need to change the default shared pointer Mode for the whole plugin.

For reference, see the implementation of MakeShared in SharedPointer.h. The Mode is set to ESPMode::Fast by default, which will be thread safe only if FORCE_THREADSAFE_SHAREDPTRS (or PLATFORM_WEAKLY_CONSISTENT_MEMORY) is set to 1.

…ts in parallel, which will construct TSharedPtr instances using the default Mode (=ESPMode::Fast), we have to enforce thread-safe shared pointers for the whole plugin.
@spr1ngd spr1ngd mentioned this pull request May 9, 2022
@MarkJGx
Copy link

MarkJGx commented Jan 22, 2024

hey, @rdeioris. Is this a confirmed crash? If so I can pitch in and help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants