[C++] Code in Actor class constructor crashes the editor

I’ve an Actor class called ASplineActor where I’ve added a USplineComponent as an UPROPERTY in the header file, which is called ‘spline’.
In my ASplineActor constructor, I did a test and added the line:
spline->SetClosedLoop(false).

I know this may look very ugly programming wise, as I guess the spline object is not allocated and initiated yet.
I did this only for testing, as I’m roughly learning to code and understand how UE4 works.

Anyway the whole editor crashes and it is impossible to load the project after I added this line.
Removing the line, and also removing the build and binaries folder, everything is fine.

Hey Vantskruv-

This is because you’re trying to make changes to the component before the component has been created. If you add spline = CreateDefaultSubobject(TEXT("spline")); to the constructor prior to the SetClosedLoop() call then you should be able to compile the project successfully.

Cheers

Thanks for your answer! I’ve already solved it by allocating USplineComponent. If remember it correctly I did it in a different way, but your example looks much more cleaner.

Though the main question is if this behaviour of the editor should be treated as normal or as a bug, as being not able to open the project via the editor may be quite severe i.e. you don’t know what went wrong (i’m not sure if this is debuggable, maybe I’ve set something wrong…). Or should I treat the editor as a part of my project, and using VC as a base? (i.e. is it preferable to compile and start the editor from VC, as I’ve seen you can compile and then start an instance of the editor from VC).

Both options would work for development. Running through VS would allow you to debug problems that occur in the editor since it attaches Visual Studio’s debugger however this isn’t as performant usually.