C++ UObject Garbage Collection Clarification

I understand that any class which inherits from UObject has the benefit of automatic garbage collection.

However if an AActor (which is a UObject) creates an OpenCV cv::Mat object (which is not a UObject) and stores the cv::Mat within the AActor, if the AActor is destroyed will the cv::Mat object also be destroyed and gargage collected? Or will I have to manually destruct and garbage collect cv::Mat first before destroying AActor?

In other words, does UObject’s garbage collection apply only to the original UObject, or does it extend to all objects and components (even thrd-party components which are not UObject) stored within the class?

If you create non-UObject with new then yes, it is your responsibility to free up memory. (take a look on TSharedPtr<> or TUniquePtr<>).
It doesn’t matter if it is a class member or a local variable in a function.

If you’ve got a cv::Mat member (not a pointer) then you’re fine.

Not sure what level of knowledge you have of C++ in general, but suffice to say that the garbage collector calls the UObject’s destructor, so if you’re managing heap-allocated memory with smart pointers, you’ll be fine. If for some reason you really want a naked pointer to an object you created with new then you can delete it in an override of UObject::BeginDestroy.

Thanks @Emaer @anonymous_user_4aa5607e1.

I’ve had a look into Shared Pointers and Shared References - it seems like this is exactly what I need.