Download

Сrash UE4 when creating an instance of an object

Hi Guys,

I’m new and I have a problem:

I create an instance of any class, even empty in some function called via the keyboard in the game. When I press the button, the UE4 crashes. Maybe the instances cannot be created in Character.cpp?

New objects must be created in a valid package and must have a valid outer.
If you break any of the above rules then engine crashes.

Well, how can I find out if my object is in a valid package and how can I move it there?

Set outer as current level’s UWorld is an easy step.

There is a chance that your instance got automatically garbage collected ? You should try to use persistent data variables, so they don’t get away out of memory block, which can make the engine unstable and crashes

The fact is that I create an instance of an absolutely empty class just created, in which there is no garbage!

Here are some screenshots showing the problem.

You can’t create an Actor that way. You need to use SpawnActor. Actor’s aren’t trivially constructed objects.



void ATestCharacter::CollectHP()
{
  AExem* NewExemInstance = GetWorld()->SpawnActor<AExem>(AExem::StaticClass(), GetActorTransform());
}


Get used to Unreal crashing a lot, sometimes for not-so-obvious reasons. I recommend installing the debug symbols so you at least have a stacktrace to debug. In this case, it looks like the error has already been pointed out, but it is one of those things where Epic could do some sanity-checking on their part and log an error instead of letting the editor/game crash.