Can I override or extend the destructor of a UObject subclass?

I have created a UObject subclass that wraps an object from an external library. I have a “normal” C++ pointer to the external object, and I can initialize that object whenever I first need it (by calling the appropriate external creation method).

I can also clear the pointer in the copy constructor (so that the copied UObject will create its own new object when needed).

But what I can’t work out is how to call the external library destructor method when my UObject is garbage collected. And I really want to call the external library object free method, because insde the object there’s open sockets, and a thread, and a lot of other stuff that I really want to go away when my UObject is no longer used.

Overriding the C++ destructor doesn’t seem to work (compiler errors) and searching the net also hasn’t turned anything up.

So either I’m searching for the wrong keywords, or I should be going about what I want to do in a completely different way.

Can anyone enlighten me?

(And please note that even though I have 30+ years of C++ developer experience I am a Unreal noob, so in case I’m asking a bleedin’ obvious question please point me in the right diretion…)

Hi there,

You can perhaps override this: virtual void BeginDestroy();

Disclaimer: Same boat as you, old timer, unreal noob. I did not test this, just looked for likely virtuals :wink:

Thanks, that was a really useful pointer! I’m not sure whether it is the best option (at least for my use case), but your mention of BeginDestroy() made me find the “Actor Lifecycle” document, which seems to suggest that UninitializeComponents() is what I need to look at.

I’ll report here when I have something working.

1 Like