# Stale TWeakObjectPtr

**URL:** <https://forums.unrealengine.com/t/stale-tweakobjectptr/38449>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [August 13, 2015, 9:11pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449 "2015-08-13T21:11:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerry.Richards](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jerry.Richards](https://forums.unrealengine.com/u/Jerry.Richards)\
**Post date:** [August 13, 2015, 9:11pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/1 "2015-08-13T21:11:58Z")

</div>

Initially I instantiated a class from my GameMode and accessed in other parts of the game. This appeared to work well. Since making use of FStreamableManager for loading assets, I decided to move several items into GameInstance. The problem I’m having now though is that the TWeakObjectPtr becomes Stale for no apparent reason:

 ![Stale.TWeakObjectPtr.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/d/1/d1494f1bbd06d07aede7d7ab266ab1db7462b05c.jpeg)

What is the proper way to instantiate a TWeakObjectPtr? Is the below valid:

```auto

Manager = TWeakObjectPtr<UMyManager>(NewObject<UMyManager>(UMyManager::StaticClass()));

```

What about returning the pointer to other classes:

```auto

TWeakObjectPtr<UMyManager> UMyGameInstance::GetMyManager()
{
    return Manager;
}

```

What about AddRef()?

How can I make it so that the members of my class are not going out of “scope”, being STALE?

---

<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:** [August 14, 2015, 7:28am UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/2 "2015-08-14T07:28:06Z")

</div>

Why are you using a weak pointer? The whole point of it is that it will not prevent the object from being garbage collected (and when that happens, the weak pointer will be marked as stale). Clearly for a manager class, you want it to persist, so you shouldn’t store it in a weak pointer.

Just have

```auto

UPROPERTY()
UMyManager* Manager;

```

in your game instance, and assign it as follows

```auto

Manager = NewObject<UMyManager>();

```

You can then return it in GetMyManager as a raw UMyManager\*, since you can assume that the game instance will be around as long as any caller, so it won’t be garbage collected.

---

<div class="post-metadata">

**Author:** ![Jerry.Richards](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jerry.Richards](https://forums.unrealengine.com/u/Jerry.Richards)\
**Post date:** [August 14, 2015, 12:40pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/3 "2015-08-14T12:40:26Z")

</div>

Thank you Kamrann for confirming that for me. After reading another thread someone else mentioned using UPROPERTY() for persistence reasons. And I have already switched to the syntax you described above. Then I found class members, those objects managed by the manager, going out of scope (FText for example). So I made almost everything a UPROPERTY() in the managed objects too. Is this correct then?

---

<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:** [August 14, 2015, 12:55pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/4 "2015-08-14T12:55:03Z")

</div>

What do you mean by going out of scope?  
Only UObjects will be garbage collected. If your manager is stored as a UPROPERTY it will be fine, so anything embedded in it that isn’t a UObject (such as an FText) will also persist just fine. The only reason to make those UPROPERTY also would be if you want to access the members in the editor/via blueprint.

---

<div class="post-metadata">

**Author:** ![Jerry.Richards](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jerry.Richards](https://forums.unrealengine.com/u/Jerry.Richards)\
**Post date:** [August 14, 2015, 2:43pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/5 "2015-08-14T14:43:03Z")

</div>

Without UPROPERTY on the manager, the manager goes out of scope. I just reverted my code back to the following:

```auto

.h
    UMyManager* Manager;

.cpp
    :
    Manager = NewObject<UMyManager>();
    :

    UMyManager* UMyGameInstance::GetManager()
    {
        return Manager;
    }

```

And while accessing it from code derived UUserWidget:

```auto

void UMyWidget::Update()
{
    gameInstance = (UMyGameInstance*)(GetWorld()->GetGameInstance());
    int32 item = gameInstance->GetManager()->GetSomeItem();

```

The debugger shows that the Manager object has gone out of scope:

![a08e50f37498eabcbb25dff3198bb4741097ad89.jpeg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/0/8/a08e50f37498eabcbb25dff3198bb4741097ad89.jpeg)

---

<div class="post-metadata">

**Author:** ![Jerry.Richards](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jerry.Richards](https://forums.unrealengine.com/u/Jerry.Richards)\
**Post date:** [August 14, 2015, 2:59pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/6 "2015-08-14T14:59:35Z")

</div>

Additionally, after adding UPROPERTY() to the Manager the UObjects managed by it appear to go out of scope:

 ![d33b6abd6d28e319284b14bc5fea8d9256255917.jpeg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/3/3/d33b6abd6d28e319284b14bc5fea8d9256255917.jpeg)

This error is due to subobjects going out of scope:

![4bd1a0e7a84f64db4fa0ada8ac974aa218593b9f.jpeg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/b/d/4bd1a0e7a84f64db4fa0ada8ac974aa218593b9f.jpeg)

---

<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:** [August 14, 2015, 3:10pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/7 "2015-08-14T15:10:45Z")

</div>

Sorry, yeah you definitely need UPROPERTY on the manager itself.

I can’t tell from that image what is causing the other issue. You’d need to check the call stack window in VS when the exception occurs, and find where in your own code it is being triggered (hit the Break button, open the call stack debug window, and try double clicking on the entries below the very top one until you find one inside your own code). That should give a better idea of why it’s happening.

---

<div class="post-metadata">

**Author:** ![Jerry.Richards](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jerry.Richards](https://forums.unrealengine.com/u/Jerry.Richards)\
**Post date:** [August 14, 2015, 3:29pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/8 "2015-08-14T15:29:29Z")

</div>

Thanks. I’ll let you know if I find the root of the problem.

---

<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:** [August 14, 2015, 3:32pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/9 "2015-08-14T15:32:30Z")

</div>

Hang on I didn’t see your last image before. What is this array with two elements? If your manager embeds other UObjects inside it, either directly or through an array/struct, those will need to be UPROPERTY as well.

Basically, any UObject-derived member (or array thereof) needs to be UPROPERTY to keep it from being garbage collected. Other type such as FText etc don’t need to be, but should be anyway if you want editor/blueprint access to them.

---

<div class="post-metadata">

**Author:** ![Jerry.Richards](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jerry.Richards](https://forums.unrealengine.com/u/Jerry.Richards)\
**Post date:** [August 14, 2015, 4:17pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/10 "2015-08-14T16:17:13Z")

</div>

Thank you 🙂 I thought I was going crazy. Yes, they are UObjects that were garbled up. I’ll make sure to make a note of this requirement for UObject members.

If I posted another question, would you be able to help me with FArchive problem?

---

<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:** [August 14, 2015, 4:26pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/11 "2015-08-14T16:26:27Z")

</div>

Most probably not because I’ve never used it. Post away anyway (in a new thread), I’ll take a look, and someone may be able to help.

Also check [this](https://forums.unrealengine.com/showthread.php?71341-Unreal-Slackers-The-unofficial-Slack-chat-for-Unreal-Engine-developers&p=300807&viewfull=1#post300807) out for another good learning resource.

---

<div class="post-metadata">

**Author:** ![Jerry.Richards](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jerry.Richards](https://forums.unrealengine.com/u/Jerry.Richards)\
**Post date:** [August 14, 2015, 5:02pm UTC](https://forums.unrealengine.com/t/stale-tweakobjectptr/38449/12 "2015-08-14T17:02:35Z")

</div>

Thanks, that is pretty cool.
