# Clarification about GC and NewObject\<\>

**URL:** <https://forums.unrealengine.com/t/clarification-about-gc-and-newobject/315111>\
**Category:** C++\
**Tags:** newobject, memory-management, unreal-engine, CPP, question\
**Created:** [April 15, 2015, 3:33am UTC](https://forums.unrealengine.com/t/clarification-about-gc-and-newobject/315111 "2015-04-15T03:33:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_5c81321f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5c81321f](https://forums.unrealengine.com/u/anonymous_user_5c81321f)\
**Post date:** [April 15, 2015, 3:33am UTC](https://forums.unrealengine.com/t/clarification-about-gc-and-newobject/315111/1 "2015-04-15T03:33:29Z")

</div>

I have a plugin module where the implementation class is derived from custom derivation of IModuleInterface. The module loads fine and is accessible. Inside the module implementation I have a private regular C++ pointer (not decorated with UPROPERTY because after all this class is a module class and not an UObject) to a custom UObject. Inside StartupModule I allocate an instance of that object using NewObject\<\>.

Is this sufficient for keeping said private UObject alive? NewObject\<\> assigns a default Outer object, no? And so isn’t that good enough to keep it alive from the GC? I test the persistence by calling GetWorld()-\>ForceGarbageCollection(true) inside my custom PlayerController in the main game module repeatedly through a triggered UFUNCTION, and the UObject in question seems to stay accessible. I am just getting lucky or is what I am doing legit?

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [April 15, 2015, 6:48am UTC](https://forums.unrealengine.com/t/clarification-about-gc-and-newobject/315111/2 "2015-04-15T06:48:56Z")

</div>

As I understand things that shouldn’t be safe, I didn’t think that the outer contributed to reference counting by default. So I’m surprised it’s working. Anyway, to be safe, just call `AddToRoot()` on your object immediately after creating it. This will make sure it remains referenced for the duration your program runs.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5c81321f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5c81321f](https://forums.unrealengine.com/u/anonymous_user_5c81321f)\
**Post date:** [April 16, 2015, 12:53am UTC](https://forums.unrealengine.com/t/clarification-about-gc-and-newobject/315111/3 "2015-04-16T00:53:44Z")

</div>

Got it. Will do, although I hope one day UE4 memory rules and structure will be clarified further.
