# Unreal c++ memory safety - dangling pointers

**URL:** <https://forums.unrealengine.com/t/unreal-c-memory-safety-dangling-pointers/52357>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [January 14, 2016, 12:23am UTC](https://forums.unrealengine.com/t/unreal-c-memory-safety-dangling-pointers/52357 "2016-01-14T00:23:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_a6661c981](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a6661c981](https://forums.unrealengine.com/u/anonymous_user_a6661c981)\
**Post date:** [January 14, 2016, 12:23am UTC](https://forums.unrealengine.com/t/unreal-c-memory-safety-dangling-pointers/52357/1 "2016-01-14T00:23:34Z")

</div>

Couldn’t google a definite answer so I hope I’ll get one here.

My question is this. In straight c++ if I had an object of class A that had a member B\* that pointed to some object, once that object got deleted by something I was in trouble if I didn’t implement some kind of mechanism that would clear all the pointers to it.

Now I’m wondering how Unreal’s garbage collector, reflection and other custom stuff affect that situation. Will IsValid() check save me, or do I still have to be careful about this?

Thanx!

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [January 14, 2016, 12:50pm UTC](https://forums.unrealengine.com/t/unreal-c-memory-safety-dangling-pointers/52357/2 "2016-01-14T12:50:45Z")

</div>

If the Pointer is a UPROPERTY then the Garbage Collector handles it itself, and doesn’t allow that object to be fully deleted from memory if it’s referenced by something else (I believe). if the pointer isn’t a UPROPERTY then UE’s GC has no idea and you’ll get all the usual quarms with null pointers.

What you probably want is a TWeakObjectPtr - which allows the pointed-to object to be fully deleted and automatically nulls the pointer.

---

<div class="post-metadata">

**Author:** ![HateDread](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HateDread](https://forums.unrealengine.com/u/HateDread)\
**Post date:** [January 14, 2016, 1:34pm UTC](https://forums.unrealengine.com/t/unreal-c-memory-safety-dangling-pointers/52357/3 "2016-01-14T13:34:00Z")

</div>

The exception is if the classes to which you point are not in the UObject hierarchy, right, @TheJamsh? Like, if you’re new-ing them manually, I would hope that the GC would leave them alone.

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [January 15, 2016, 9:39am UTC](https://forums.unrealengine.com/t/unreal-c-memory-safety-dangling-pointers/52357/4 "2016-01-15T09:39:20Z")

</div>

Yeh I believe that’s correct too 🙂
