question

Jørgen Havsberg Seland avatar image
Jørgen Havsberg Seland asked

Invalid use of UObjects in PlayFab Unreal Engine SDK

The PlayFab Unreal SDK uses a mix of UPROPERTY and TSharedPtr to reference UObjects. This is not valid use of the unreal memory systems.

UObjects in unreal are garbage-collected, and should only be referenced via UPROPERTY, via fields listed in the the AddReferencedObject callbacks, or via TWeakObjectPtr or TStrongObjectPtr. These references are known by the GC and are used when checking for reachability, and when collecting objects and nulling references to collected objects.

Handling these via TSharedPtr will have two systems competing over the same memory allocation, and inevitably resulting in crashes. TSharedPtr is also not known to the GC, and will not be reset to null when the object is collected. Rather, the TSharedPtr will point to deallocated (or even reallocated) memory.

A patch for this was introduced a while ago where the deleter used in the TSharedPtr instances was overridden to destroy the object via the GC heap, and the instances were created with the GC standalone flag. This worked fine as long as the UObjects (the authentication context) were only referenced via TSharedPtr.

With more recent additions to the SDK, the UObjects are now referenced both via TSharedPtr and UPROPERTY, and are not always created with the standalone flag.

This results in premature garbage collection and the pointers inside TSharedPtr are left dangling.

We have patched this by replacing all occurrences of TSharedPtr referencing a UObject with TStrongObjectPtr. This solves the issue and is correct from the POV of the UE garbage collector.

Without this patch, we had frequent crashes due to PlayFab objects being prematurely collected, and dangling references to said objects, all from within the PlayFab SDK.

sdksunreal
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image
Seth Du answered

Thanks for the feedback. I will submit a report to the team. In the meantime, please feel free to join the discussion in this thread.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jørgen Havsberg Seland avatar image Jørgen Havsberg Seland commented ·

For those needing a quick fix, the answer suggested by Markus Henschel at the bottom is a possible minimal impact approach:
https://community.playfab.com/comments/39862/view.html

0 Likes 0 ·
Jørgen Havsberg Seland avatar image Jørgen Havsberg Seland commented ·

Please note that in it's current state, the Unreal SDK is not stable out-of-the-box.

0 Likes 0 ·
franklinchen avatar image
franklinchen answered

Thank you for your suggestion, we have published a fix on this: https://github.com/PlayFab/UnrealMarketplacePlugin/pull/29/commits

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.