Actor Component gets destroyed soon after creation (Garbage Collector?)

Hello,

In our project’s code we have an Actor-derived class EternalCharacter:

36298-asdfasd.png

It has a StatComponent uproperty (StatComponent is derived from ActorComponent):

36297-sdfas.png

We initialize StatComponent this way in EternalCharacter’s constructor:

Now, we derive two classes from Character class: EternalPlayer and EternalBot. Somewhere after the StatComponent creation in Char’s constructor, the StatComponent gets destroyed (presumably by Garbage Collector). Below are two screenshots from debugging, each is in several milliseconds after another, theoretically.

36311-dfasdf.png

Why would the component get destroyed nearly immediately after it gets created? Do we need to call registerComponent or something like this?

P.S: We are on 4.6.1

I’m experiencing troubles with the GC too.
It collects my object (saved via a UPROPERTY), as well as the MovementComponent (the default one, not a custom one).

Hello, Lordink

To prevent component from being garbage collected in this situation, you can use RegisterComponent() function:
StatsComponent->RegisterComponent();

Please note, that it should be called in the appropriate context, which means that “this” points to the owning Actor, that creates the component.

Thus, RegisterComponent() provides correct performance only when “this” pointer is valid and has a valid world associated with itself, which works for most Actors.

Hope this helped!