# Is it safe to use sharedptr to act as Uobject hard reference?

**URL:** <https://forums.unrealengine.com/t/is-it-safe-to-use-sharedptr-to-act-as-uobject-hard-reference/119248>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [November 26, 2018, 1:23pm UTC](https://forums.unrealengine.com/t/is-it-safe-to-use-sharedptr-to-act-as-uobject-hard-reference/119248 "2018-11-26T13:23:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Commander\_Shepard](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Commander\_Shepard](https://forums.unrealengine.com/u/Commander_Shepard)\
**Post date:** [November 26, 2018, 1:23pm UTC](https://forums.unrealengine.com/t/is-it-safe-to-use-sharedptr-to-act-as-uobject-hard-reference/119248/1 "2018-11-26T13:23:11Z")

</div>

Is it safe to do something like the following ( don’t ask why :p) :

TSharedPtr\<UObject\>(NewObject\<MyObjectClass\>(SomeOwner));

or are smart pointers only for non uobjects and every Uobject derived class should be maintained exclusively only by reflection system?

---

<div class="post-metadata">

**Author:** ![ExtraLifeMatt](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extralifematt/32/918695_2.png) [@ExtraLifeMatt](https://forums.unrealengine.com/u/ExtraLifeMatt)\
**Post date:** [November 26, 2018, 2:38pm UTC](https://forums.unrealengine.com/t/is-it-safe-to-use-sharedptr-to-act-as-uobject-hard-reference/119248/2 "2018-11-26T14:38:33Z")

</div>

> [@](#):
>
> Limitations
> 
> - Shared pointers are not compatible with Unreal objects (UObject classes)!

> **[Unreal Smart Pointer Library](https://docs.unrealengine.com/5.3/en-US/smart-pointers-in-unreal-engine/)**
>
> Custom implementation of shared pointers, including weak pointers and non-nullable shared references.

There is a TWeakObjPtr you can use to store a weak uobject reference, but otherwise have to go through the UPROPERTY system.

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [November 26, 2018, 2:38pm UTC](https://forums.unrealengine.com/t/is-it-safe-to-use-sharedptr-to-act-as-uobject-hard-reference/119248/3 "2018-11-26T14:38:49Z")

</div>

No, don’t do that.  
We have TWeakObjectPtr\<\> for that case.

I don’t know if that TSharedPtr\<UObject\> would even compile. When I tried some year ago, Unreal just said “no!”

---

<div class="post-metadata">

**Author:** ![Balgy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Balgy](https://forums.unrealengine.com/u/Balgy)\
**Post date:** [November 26, 2018, 2:41pm UTC](https://forums.unrealengine.com/t/is-it-safe-to-use-sharedptr-to-act-as-uobject-hard-reference/119248/4 "2018-11-26T14:41:05Z")

</div>

> [@Commander\_Shepard](#):
>
> Is it safe to do something like the following ( don’t ask why :p) :
> 
> TSharedPtr\<UObject\>(NewObject\<MyObjectClass\>(SomeOwner));
> 
> or are smart pointers only for non uobjects and every Uobject derived class should be maintained exclusively only by reflection system?

Unreal uses a garbage collection system to do its memory management, so you can also take advantage of that.

If there is a fundamental need for your object to not be destroyed, there is a way to add an object to GC’s root set: [https://api.unrealengine.com/INT/API…oot/index.html](https://api.unrealengine.com/INT/API/Runtime/CoreUObject/UObject/UObjectBaseUtility/AddToRoot/index.html)
