Calling NewObject() causing crash

Hey, I’m mucking around with the ShooterGame sources and I want to create a sub-uobject of AShooterWeapon to handle recoil. I want this to be a UObject so it can set timers and receive ticks. However, whenever I allocate my object I am getting a crash:

class AShooterWeapon
{
private:
  UPROPERTY()
  UObject* m_recoilManager;
};
AShooterWeapon::AShooterWeapon(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer), m_recoilManager(nullptr)
{
  m_recoilManager = NewObject<UObject>(); // crash in a UObject method somewhere in engine (no debugging symbols so don't know where exactly right now)
}

When I compile and run this in the editor, it crashes after a second or two. It doesn’t crash at the NewObject() itself, but somewhere down the track.
I’m using UPROPERTY() on the member variable so that the allocated UObject doesn’t get garbage collected.

I can’t imagine what I could be doing wrong. Any ideas? Thanks!

You would probably know whats wrong if you would read log in Saved/Logs, UE4 as assertion system and selfchecking if something is wrong with the code it will crash it self with reason written in logs.

There 2 issues with your code, first you trying to create object of UObject class… UObject is code object class and abstract, possibly code in there notice that and crush the engine. You should put your recoil manager class there insted of UObject.

2nd issue is that you creating object in constructor. Constructors in UObjects works differently due to fact how reflection system works in UE4, when engine starts it create default copies of all UObject classes (called CDO) which contain default variables and they are used as stamps to create new objects (at least i think). This is why constructor should not have any gameplay code and only contains defaults and component information (in case of actors), otherwise you code will behave strangely (it will create object in default copy) or you will have crash on engine start as not all is initiated that stage. UObject has set of event function which you can override which are called on varius object/actor creation stages, all object creation should happen at BeginPlay() when actor ends initiation or in case of UObject PostInitProperties() when you sure that properties are already set correcly.

Thanks for your help.