The editor runs the ActorComponent fine the first time I hit play, but if I hit play a second time the editor crashes. I have BeginPlay implemented on the abstract Base class, and subclasses since if I print to the output log the editor doesn’t crash. (Code shown doesn’t have it)
I don’t think there’s really any reason to have TickComponent, or BeginPlay on these classes. (Works fine the first time I hit play without crashing) I’ve tried tons of things from manually deleting Interfaces, to migrating my Interfaces to Unreal’s implementation of interfaces. I also tried changing where Banter2afterthespiders intializes it’s values from the Constructor to OnRegister.
memreport shows memory usage growing slightly bewteen the first play and second play, I have a feeling that my c_0 TFunction isn’t being garbage collected, but I’m not sure how to either delete a TFunction manually or allow for a Union to be garbage collected. If anyone is able to help me figure out what’s causing the crash I’d appreciate it greatly.
It is probably indeed garbage collection related. If you’re saving pointers to a UObject in a member variable, you have to remember to mark those variables with UPROPERTY(). UE uses reference counting for its garbage collection so if you’re storing a UObject pointer in a field that isn’t marked UPROPERTY, it won’t count as a reference and that object may be garbage collected too early. In your case try making ‘gameTime’ or ‘branch’ as UPROPERTY and see if it makes a difference.
So, I guess I need to manually clear structs first. I don’t see a way to do that in the documentation. Do I just need to run the destructor manually? branch can’t be set as a UPROPERTY(), since TUnion doesn’t support that attribute yet.
Are you sure you can’t mark it with UPROPERTY without any specifiers? I haven’t worked with TUnion so I believe you, just want to make sure you didn’t try it with any specifiers like VisibleAnywhere. One workaround for making sure objects that the struct stores are reference counted is by keeping an extra TArray marked UPROPERTY() that simply stores UObject pointers that are in use in that TUnion. I used that when TMaps couldn’t be marked with UPROPERTY yet (they can nowadays).
You don’t need to run the struct’s destructor manually. I don’t think your crash is caused by something not being garbage collected. Rather, I think something is being garbage collected too soon. Whatever you did in that log operation may have triggered the object to live just a bit longer.
No, Unreal’s garbage collection only affects UObjects. Raw pointers are your own responsibility. Did you have any luck by keeping an extra TArray to reference UObjects that are stored in your TUnion? By having them in the TArray marked UPROPERTY() it guarantees them not being garbage collected, whereas your TUnion isn’t reference counted.
TArray most likely isn’t the issue. I have sublclasses of BaseDialogue, with just TArrays in the union that don’t cause the crash. What seems to be the problem is my subclasses that have TFunctions stored in the union.