I’m using ::NewObject<UBlah>(GetTransientPackage(), cls) to construct my object. I then created a TSharedPtr<UBlah> 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<UBlah> 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.