ue-memory-and-gc

Manage UObject lifetime and plain C++ memory in Unreal Engine 5.8.

55|5|Updated Mar 26, 2026
One-click install
npx skills add https://github.com/kevinpbuckley/unreal-engine-skills --skill ue-memory-and-gc-kevinpbuckley
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ue-memory-and-gc
Source: https://github.com/kevinpbuckley/unreal-engine-skills/tree/main/skills/core/ue-memory-and-gc
Command: npx skills add https://github.com/kevinpbuckley/unreal-engine-skills --skill ue-memory-and-gc-kevinpbuckley

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Unreal Engine has two memory worlds — garbage-collected UObjects and plain C++ heap objects — and mixing them up causes most UE crashes, from dangling pointers after GC to uncollectable reference cycles. This Skill gives an agent the exact rules, pointer types, and engine-source citations to choose correct ownership and debug memory bugs. ## Core Features & Use Cases - Pointer type selection: Decision guidance for TObjectPtr, TWeakObjectPtr, TSoftObjectPtr, TStrongObjectPtr, TSharedPtr, TSharedRef, TWeakPtr, and TUniquePtr, with a comparison table of GC visibility and ownership semantics. - GC internals: Explains the reachability cycle, root set, clustering, incremental GC, and the BeginDestroy/FinishDestroy destruction callback sequence. - Non-UObject ownership: Patterns for holding UObjects from plain F* classes via FGCObject::AddReferencedObjects or TStrongObjectPtr. - Use Case: An agent debugging a crash that appears seconds after a level load can identify the classic "GC collected it" symptom, find the raw UObject* member missing UPROPERTY, and fix it with TObjectPtr plus IsValid checks. ## Quick Start Use the ue-memory-and-gc skill to explain why my UObject pointer becomes garbage after a few seconds and which pointer type I should use instead.

Frequently Asked Questions about ue-memory-and-gc

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I keep a UObject alive in Unreal Engine?

Store it in a UPROPERTY member, preferably TObjectPtr<T> in UE5, so the garbage collector sees the reference from the root set. For non-UObject owners, use TStrongObjectPtr or implement FGCObject::AddReferencedObjects; AddToRoot works but must be paired with RemoveFromRoot.

Why does my UObject pointer become null or garbage after a few seconds?

The garbage collector collected the object because nothing reachable from the root set held it in a UPROPERTY. A raw UObject* member without UPROPERTY is invisible to the GC, leaving a dangling pointer; add UPROPERTY and use IsValid() checks.

What is the difference between TWeakObjectPtr and TStrongObjectPtr?

TWeakObjectPtr is a non-owning observer that auto-nulls when the target is collected, while TStrongObjectPtr keeps the target alive via GC ref-counting and is meant for non-UObject owners. TStrongObjectPtr cannot be a UPROPERTY and must not be stored in a UCLASS without one.

Can I use TSharedPtr with a UObject in Unreal Engine?

No. TSharedPtr's reference count is invisible to the garbage collector, so the GC can collect the UObject while the shared pointer still holds an address, causing a crash. Use TSharedPtr only for plain C++ F* types and UPROPERTY-based pointers for UObjects.

When should I use TObjectPtr instead of a raw pointer in UE5?

Use TObjectPtr for all UObject UPROPERTY members in UE5. It adds the GC write barrier required for incremental GC marking plus cook-time dependency tracking; raw T* UPROPERTY still compiles but does not participate in the write barrier.

How do I hold a UObject from a non-UObject C++ class?

Either inherit from FGCObject and report references in AddReferencedObjects, or store a TStrongObjectPtr for a single object. FGCObject suits dynamic collections of many objects but is not trivially relocatable, so never store it by value in a TArray.