C++ TObjectPtr<> as a return type, is it good practice?

Returning TObjectPtr<> should be safe but not something I would recommend doing. TObjectPtr<> represents a lifetime dependency similar to standard c++ smart pointers, where the object referenced in the property should either not exist without the object holding it or the object itself depends on the referenced object, hence it not being GCed for as long as the reference exists.

We typically only use TObjectPtr<> in properties and then pass the objects around using raw pointers/references. In most functions, you don’t care about lifetimes since you are not freeing anything explicitly, it makes the signatures shorter/easier to read and you can convert between TObjectPtr<> and raw pointers either implicitly or easily using Get().

1 Like