Download

Smart Pointers?

Hey so can someone please tell me in the simplest way so I can understand, what are the smart pointers in ue4 c++ such as TSharedPtr, TWeakObjectPtr and TUniquePtr and in what situation should I use each one. Any help is much appreciated, thank you :slight_smile:

Unreal Smart Pointer Library | Unreal Engine Documentation

How To Prevent Crashes Due To Dangling Actor Pointers - UE4: Guidebook (gg-labs.com)

Thanks for the links but I really don’t understand the unreal docs way of defining pointers thats why I asked for definitions and uses as simply as possible :slight_smile:

In the first link there is a section that shows why to use them as succinctly as possible.

Benefits of Smart Pointers
[TABLE]

		**Prevents memory leaks**
		Smart Pointers (other than Weak Pointers) automatically delete objects when there are no more shared references.
	
	
		**Weak referencing**
		Weak Pointers break reference cycles and prevent dangling pointers.
	
	
		**Optional Thread safety**
		The Unreal Smart Pointer Library includes thread-safe code that manages reference counting across multiple threads. Thread safety can be traded out for better performance if it isn't needed.
	
	
		**Runtime safety**
		Shared References are never null and can always be dereferenced.
	
	
		**Confers intent**
		You can easily tell an object owner from an observer.
	
	
		**Memory**
		Smart Pointers are only twice the size of a C++ pointer in 64-bit (plus a shared 16-byte reference controller). The exception to this is Unique Pointers, which are the same size as C++ pointers.

In C++ you need to manage the objects on the heap manually. But depending on where your objects are used, its not possible to know if it is valid to delete the objekt yet. Also you need to remember to delete them.

Smart pointers simply prevent those issues by wrapping the pointer in a class with management functionality. Smart Pointers are not spefic to Unreal. C++ has smart pointer classes per default. There is enough documentation on them online.

I know this kind of thing is not very well-explained. Here is a quick rundown on the ones you asked about:

TSharedPtr is a hard reference to an object that multiple other objects share communally. UE keeps track of the number of references to a given object this way and when all references to an object are released (manually) then the referenced object is destroyed. This type of reference can be null. TSharedRef works similarly but is always expected to be valid. Use TSharedPtr when declaring a variable in a class that will reference something else, like a widget perhaps, but make sure you check a TSharedPtr for validity before using it. If you’re declaring a defining a reference at the same time you can use TSharedRef. If it’s a local ref inside a function, it will be destroyed at the end of the function. If it’s a class variable, make sure you release TSharedPtrs manually at the end of the container class’ life cycle to avoid dangling pointers.

TWeakObjectPtr works like TSharedPtr but it doesn’t increment the reference count. If all other TSharedPtrs to an object are released or destroyed but you still have a TWeakObjectPtr to that object then the object in question will still be destroyed because its reference count will be 0 because the TWeakObjectPtr doesn’t count. You should still release TWeakObjectPtrs when you’re done with them, but the lifetime of the referenced object will not be affected by a TWeakObjectPtr. A weak pointer is basically an object that tells you information about a raw pointer, like whether or not it’s been deallocated, without affecting the lifetime of the object the raw pointer points to.

TUniquePtr sort of locks an object to a single reference that manages it. If an object has a TUniquePtr wrapped around it, no other object can reference it unless ownership is explicitly passed to another managing object. You can use this to make sure only one object controls the lifetime of another. When a TUniquePtr is destroyed or reassigned, the wrapped object is also destroyed.

Try not to pass smart pointers as parameters, though. Just pass raw pointers. Save smart pointers as class or function members depending on your needs.

What you meant there is TWeakPtr, which like TSharedPrt, TSharedRef, and TUniquePrt, shouldn’t be used with UObjects.

TWeakObjectPtr is meant to be used with UObject, and works similar to TWeakPtr.

Yes, that’s what I meant, thank you. There’s also a TWeakFieldPtr and TWeakInterfacePtr, each for their own types of objects.

I couldn’t thank you enough for all of this information, I now understand much more than I thought I could about this topic :slight_smile: