[Repro Included] Constructor crashes project

Was following the BatteryCollector C++ tutorial.

During lesson 5 (link text), a new class, BatteryPickup was added derived from Pickup. When she wrote the constructor for BatteryPickup, I noticed she didn’t call the constructor from Pickup, so I added it, and as soon as I compiled it, it would crash the project. And crash subsequent loads of the project. I had to remove it and compile it in Visual Studio, then open the project.

ABatteryPickup::ABatteryPickup()
{
        APickup::APickup(); <--- THIS TRIGGERS CRASH
	GetMesh()->SetSimulatePhysics(true);
}

I imagine this will be easy to fix, but can someone explain why this would crash? Or why this would be improper. Is it possible, UE is doing macro magic that makes this unecessary?

I have no idea what that would actually do, but it’s not the right way to call a super class’s constructor. Any super classes have their default constructor (the one with no parameters) called automatically. If they don’t have a default constructor or you want to call one of the other constructors you put it in the object initializer list instead, like so:

 ABatteryPickup::ABatteryPickup()
    : Super()
 {
     GetMesh()->SetSimulatePhysics(true);
 }

Of course, how silly. However UE4 seems to think this is valid and it’s a good way to make it explode.

Doing it in the bad way above calls the base constructor twice, which is never desired. Calling it in the the initializer list works properly, and not calling it at all calls the default constructor properly as well.

So yeah, basically never do this. However it is crashing UE4 so it should probably be fixed. I had a simple brainfart when I did this.

Visual Studio in a traditional C++ project (no UE4) happily lets you do this without any warnings or errors.

Thanks, there is always a small level of idiot proofing that needs to happen. Actually can’t believe that’s valid C++.

Hello morness,

I was able to reproduce this issue on our end. I have written up a report (UE-28672) and I have submitted it to the developers for further consideration. I will provide updates with any pertinent information as it becomes available. Thank you for your time and information.

Make it a great day