Skip to content

TSharedPtr thread safety - #11

Open
mlavik1 wants to merge 1 commit into
rdeioris:masterfrom
mlavik1:tsharedptr-crashfix
Open

mlavik1 wants to merge 1 commit into
rdeioris:masterfrom
mlavik1:tsharedptr-crashfix

Conversation

@mlavik1

@mlavik1 mlavik1 commented Jun 30, 2021 •

Copy link
Copy Markdown

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

MarkJGx commented Jan 22, 2024

Copy link
Copy Markdown

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