fix(codegen/unreal): eliminate UObject memory leak in reducer event handlers #3987
+50
−13
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.
Summary
NewObject<UReducer>()is called for every reducer event but never cleaned upFArgsstruct directly via newInvokeWithArgs()method instead of allocating UObjectsProblem
The generated
ReducerEventhandler inSpacetimeDBClient.g.cppcreates a new UObject for every reducer event:FArgs Args = ReducerEvent.Reducer.GetAs...(); UReducer* Reducer = NewObject<UReducer>(); // LEAK - never cleaned up! Reducer->Param = Args.Param; Reducers->Invoke...(Context, Reducer);Unreal's garbage collector cannot keep up at high frequencies. Attempts to fix with
MarkAsGarbage(),ConditionalBeginDestroy(), and transient package flags all failed because GC timing is unpredictable.Solution
Eliminate UObject allocation entirely by passing
FArgsdirectly:FArgs Args = ReducerEvent.Reducer.GetAs...(); Reducers->Invoke...WithArgs(Context, Args); // Zero allocation!The original
Invoke()methods that takeUReducer*are preserved for backwards compatibility.Changes
InvokeWithArgs()method declarationInvokeWithArgs()instead of creating UObjectInvokeWithArgs()that extracts params fromFArgsstructTest Plan
TickGameScheduledreducer - UObject count remains stableInvoke()methods still workBefore/After
Before:
OBJScount in Unreal stats continuously increasing, memory growing unboundedAfter:
UObjDelta=0- no UObject growth from reducer events