glTFRuntime icon indicating copy to clipboard operation
glTFRuntime copied to clipboard

TSharedPtr thread safety

Open mlavik1 opened this issue 3 years ago • 1 comments

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.

mlavik1 avatar Jun 30 '21 10:06 mlavik1

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

MarkJGx avatar Jan 22 '24 12:01 MarkJGx