When people are afraid that they'll forget the delete
call for standard C++ objects they create, they often use smart pointers to prevent memory leaks. TSharedPtr
is a very useful C++ class that will make any custom C++ object reference-counted—with the exception of UObject
derivatives, which are already reference-counted. An alternate class
TWeakPtr
is also provided for pointing to a reference-counted object with the strange property of being unable to prevent deletion (hence, "weak").
Tip
UObject
and it's derivative classes (anything created with NewObject
or ConstructObject
) cannot use TSharedPtr
!
If you don't want to use raw pointers and manually track deletes into your C++ code that does not use UObject
derivatives, then that code is a good candidate for using smart pointers such as TSharedPtr
, TSharedRef
,
and the like. When you use a dynamically allocated object (created using the keyword...