# TArray of TSharedRef validity

**URL:** <https://forums.unrealengine.com/t/tarray-of-tsharedref-validity/2500896>\
**Category:** Programming & Scripting\
**Tags:** reference, tarray, unreal-engine, unrealengine4, CPP, question\
**Created:** [May 10, 2025, 3:02pm UTC](https://forums.unrealengine.com/t/tarray-of-tsharedref-validity/2500896 "2025-05-10T15:02:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuiteLored](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SuiteLored](https://forums.unrealengine.com/u/SuiteLored)\
**Post date:** [May 10, 2025, 3:02pm UTC](https://forums.unrealengine.com/t/tarray-of-tsharedref-validity/2500896/1 "2025-05-10T15:02:20Z")

</div>

Hello.

Would as of 4.27 a TArray of TSharedRef automatically reflect referenced objects being destroyed by reducing number of elements or produce errors instead?

as in, if I made an array of shared references and iterate over it, is it possible that I get to invalid ones? or if it’s a bad idea, are there similar alternatives, maybe TSet or some other clean way to do it instead of additional manager?

The purpose I need it for is that I need to produce some arbitrary actors managed by another one, of a certain dynamic quantity, only local (not replicated over nets). Ideally if this was a valid solution I don’t need a manager, I just decided to ask maybe more experienced coders.

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 11, 2025, 5:12pm UTC](https://forums.unrealengine.com/t/tarray-of-tsharedref-validity/2500896/2 "2025-05-11T17:12:12Z")

</div>

Shared pointers in Unreal Engine are made for objects outside the UObject system. If you want to use this TArray with actors, shared pointers should not be used. You want to use TObjectPtr. Yes, they will be null if you destroy any of the actors that are stored in these TObjectPtr’s.

You can use the actor’s OnDestroyed event to be notified when to remove an actor from your list when they are destroyed.

---

<div class="post-metadata">

**Author:** ![SuiteLored](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SuiteLored](https://forums.unrealengine.com/u/SuiteLored)\
**Post date:** [May 12, 2025, 12:30pm UTC](https://forums.unrealengine.com/t/tarray-of-tsharedref-validity/2500896/3 "2025-05-12T12:30:08Z")

</div>

I’m not entirely sure what I am doing wrong but I get  
“42:9: e[0me[0;1;31merror: e[0me[1muse of undeclared identifier 'TObjectPtr’e[0m  
CompilerResultsLog: TArray\<TObjectPtr\> Arrows;”  
and couldn’t find much info about it in 4.27

---

<div class="post-metadata">

**Author:** ![SuiteLored](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SuiteLored](https://forums.unrealengine.com/u/SuiteLored)\
**Post date:** [May 12, 2025, 12:34pm UTC](https://forums.unrealengine.com/t/tarray-of-tsharedref-validity/2500896/4 "2025-05-12T12:34:30Z")

</div>

and, obviously “42:9: e[0me[0;1;31merror: e[0me[1muse of undeclared identifier 'TObjectPtr’e[0m  
CompilerResultsLog: TArray\<TObjectPtr\< ABeamArrow\>\> Arrows;” tried that first

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [May 12, 2025, 12:35pm UTC](https://forums.unrealengine.com/t/tarray-of-tsharedref-validity/2500896/5 "2025-05-12T12:35:16Z")

</div>

“use of undeclared identifier”, missing header include or incorrect usage.

```auto
TArray<TObjectPtr<AActor>> Test;

```

Thought TObjectPtr was actually UE5

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [May 12, 2025, 12:48pm UTC](https://forums.unrealengine.com/t/tarray-of-tsharedref-validity/2500896/6 "2025-05-12T12:48:01Z")

</div>

> [@SuiteLored](#):
>
> Would as of 4.27 a TArray of TSharedRef automatically reflect referenced objects being destroyed by reducing number of elements or produce errors instead?

Always use pointers with UObjects. (UObject\* Test = nullptr) or its TObjectPtr variant.

The garbage collector automatically nulls these pointers when it collects the garbage objects. You can easily check them for nullptr or call IsValid. You want to avoid working with references that can become invalid (undefined behavior).

> [@SuiteLored](#):
>
> automatically reflect referenced objects being destroyed by reducing number of elements

Arrays do not shrink automatically when pointers inside them null.

> [@SuiteLored](#):
>
> and iterate over it, is it possible that I get to invalid ones

outside pointers to elements inside an array become invalid when elements are added / removed in array.

> [@SuiteLored](#):
>
> The purpose I need it for is that I need to produce some arbitrary actors managed by another one, of a certain dynamic quantity, only local (not replicated over nets). Ideally if this was a valid solution I don’t need a manager, I just decided to ask maybe more experienced coders.

You should create an array of uobject pointers on the manager class. When you want to remove one of those uobjects, remove them through the manager so that the element is removed from its array before it starts to be destroyed. Then you don’t need to think about manually cleaning up nullptrs and such.
