Why is my shared pointer object being garbage collected even though I still have a reference to it?

I’m using ::NewObject&LTUBlah&GT(GetTransientPackage(), cls) to construct my object. I then created a TSharedPtr&LTUBlah&GT object that holds a reference to the object. Then when a level is switched, my object gets garbage collected. I know this because I trapped the destructor call. Why is this happening? I’m holding a reference to the object, and as proof of that, when I call the IsValid() method on my TSharedPtr&LTUBlah&GT instance, it returns true. But when I go to use it, I crash, of course, because the object was freed and so the pointer is stale.

What I may be missing here is that I don’t understand the role being played by the first argument to ::NewObject. What is the “outer” argument here? How does its scope control the scope of the object I’m creating? Doesn’t it defeat the purpose of holding a reference to my object?

By the way, Epic’s documentation on smart pointers isn’t very good.

Epic’s documentation (which I perhaps wrongly bashed) says…

“These classes cannot be used with the UObject system because Unreal Objects use a separate memory-tracking system that is better-tuned for game code.”

…at the following link…

So the answer is that my shared pointer references don’t effect the scope of my object at all. So what does? I’m left now trying to understand how the “owner/outer” parameter works, because that’s the only thing I can think of.

I’ve also learned that you can use the AddToRoot() method of UObject to keep an object around between level transitions.