How to detect circular reference of TSharedPtr?

If you make two object both have a TSharedPtr to another, it will cause a crash when release memory such as shutdown engine or editor.

But if there are already many shared pointers,it’s really hard to find the circular reference of Shared Pointers.

So is there any solutions? Can I print all reference of Shared Pointers with who hold this point and which object do they point to a picture or some text file?

There is no built-in mechanism to detect circular shared pointer references. At Epic we use conventions instead.

The various types of shared pointers imply the degree of ownership of the object being pointed to. If you’re using TSharedPtr then you’re basically saying that you are in control of that object’s lifetime. You have to think about whether such a strong ownership is actually necessary in your code, and if it is not, then you should prefer a TWeakPtr instead.

Slate, for example, is heavily using TSharedPtr internally, because the widget hierarchy implies strong ownership of child widgets, so that makes complete sense. As a convention, inside of Slate code we try to use only TWeakPtr references to non-Slate objects, or to Slate objects that are not directly owned by a widget. If you take a close look at the APIs for combo and list boxes, for example, you will notice that, although their methods take TSharedPtr instances, the internal storage of those pointers is almost always TWeakPtr.

As a rule of thumb: unless you need to control the referenced object’s lifetime, make it a TWeakPtr.

Really thanks for your good advise! And yes we meet circular reference when we make widget with slate in plug-in.I’ll try to make conventions .