Need help debugging

Hello,

The code I’m currently writing compiles fine in VS2015 but causes my UE4-Editor to crash as soon as I open it (telling me, im writing functional C++ code, but UE4 doesn’t like it).
I recieve following message:

Fatal error: [File:D:\Build++UE4+Release-4.18+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 2495] UObject(const FObjectInitializer&) constructor called but it’s not the object that’s currently being constructed with NewObject. Maybe you are trying to construct it on the stack, which is not supported.

UE4Editor_Core!FDebug::AssertFailed() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\misc\assertionmacros.cpp:414]
UE4Editor_CoreUObject!UObject::UObject() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\uobjectglobals.cpp:2496]
UE4Editor_Engine!UDataAsset::UDataAsset() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\dataasset.cpp:9]
UE4Editor_TheImmortal_Demo_7576!URotationAnatomyMap::SetupNewComponent() [c:\users\SOMEGUY\mein zeug\geheimes geheimzeug\theimmortal_demo\source\theimmortal_demo\rotationanatomymap.cpp:30]
UE4Editor_TheImmortal_Demo_7576!APlayerCharacter::APlayerCharacter() [c:\users\SOMEGUY\mein zeug\geheimes geheimzeug\theimmortal_demo\source\theimmortal_demo\playercharacter.cpp:28]
UE4Editor_CoreUObject!UClass::CreateDefaultObject() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\class.cpp:2728]
UE4Editor_CoreUObject!UObjectLoadAllCompiledInDefaultProperties() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\uobjectbase.cpp:795]
UE4Editor_CoreUObject!ProcessNewlyLoadedUObjects() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\uobjectbase.cpp:869]
UE4Editor_CoreUObject!TBaseStaticDelegateInstance<void __cdecl(void)>::ExecuteIfSafe() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\public\delegates\delegateinstancesimpl.h:1027]
UE4Editor_Core!TBaseMulticastDelegate<void>::Broadcast() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\public\delegates\delegatesignatureimpl.inl:937]
UE4Editor_Core!FModuleManager::LoadModuleWithFailureReason() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\modules\modulemanager.cpp:487]
UE4Editor_Projects!FModuleDescriptor::LoadModulesForPhase() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\projects\private\moduledescriptor.cpp:476]
UE4Editor_Projects!FProjectManager::LoadModulesForProject() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\projects\private\projectmanager.cpp:69]
UE4Editor!FEngineLoop::LoadStartupModules() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\launch\private\launchengineloop.cpp:2577]
UE4Editor!FEngineLoop::PreInit() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\launch\private\launchengineloop.cpp:1992]
UE4Editor!GuardedMain() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\launch\private\launch.cpp:127]
UE4Editor!GuardedMainWrapper() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:134]
UE4Editor!WinMain() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:210]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:253]
kernel32
ntdll

The code I believe to be responsible for this is the following:

void URotationAnatomyMap::SetupNewComponent(FName InComponentName, float InPositiveYaw, float InNegativeYaw, float InPositivePitch, float InNegativePitch, float InPositiveRoll, float InNegativeRoll)
{
	URotationAnatomyMapComponent* TempMapComponent = NewObject<URotationAnatomyMapComponent>();
	TempMapComponent->SetRotationValues(InPositiveYaw, InNegativeYaw, InPositivePitch, InNegativePitch, InPositiveRoll, InNegativeRoll);
	RotationAnatomyMapComponents.Add(InComponentName, TempMapComponent);
}

I just can’t wrap my head around this, literally sitting here for hours, trying to find the error… Do you guys knowwhat I’m doing wrong?

Thanks in advance!

Hey there, try adding this as a parameter to the component creation and try registering it after creation like this:

UPhysicsHandleComponent * PhysicsHandle = NewObject<UPhysicsHandleComponent>(this);

PhysicsHandle->RegisterComponent();

Hey thanks for the quick response!

I added the “this” keyword to my NewObject function call, the error still occurs.
The RegisterComponent function was not available, because my component inherits from UDataAsset not from UActorComponent…

I literally just explained this to another person. You cannot assign a new component to a local variable. It needs to be assigned to a member variable of the class that owns it. Components are expected to be properties of an actor. Instead of an array of components, you could use an array of actors that have the component in them.

Ah ok since it had component in the name i assumed it was an actor component. Check this to see if it helps.

I actually create new components as a local variable in a function with:

UPhysicsHandleComponent * PhysicsHandle = NewObject<UPhysicsHandleComponent>(this);

PhysicsHandle->RegisterComponent();

Maybe it’s the type of the component that influentiates that or the register component function that allows that to work. I also save them to an array that is a variable of the class.

Though the name might be confusing my “RotationAnatomyMapComponent” is not a component but a UDataAsset.

Component or not, it’s the same answer, as stated in the log “Maybe you are trying to construct it on the stack, which is not supported.”

“The stack” meaning a local variable. You can’t do that.

Hmm… It looks like NewObject() isn’t recursion-safe. In other words, you can’t call NewObject() to create an object from inside the constructor of another object being created. At least, that’s what it looks like to me.

Is the SetupNewComponent() function being called during the construction of a URotationAnatomyMap object? Edit: Oh, I see it’s being called during the construction of your player actor. Maybe this is the issue then.

Yep, I think this is it. I can see in the code where it’s failing. (Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp:2495)

It looks like it uses the thread as a kind of global context for constructing UObject-derived classes. So when you create the new object it’s saying that you’re already in the process of constructing an object.

Try doing what xlar8or suggests: change over to using CreateDefaultSubObject() rather than NewObject().

Hmm… Though these aren’t sub-objects. I guess the other option is to not do this in the player constructor.

That makes sense, usually in the constructor you use CreateDefaultSubobject.

Seems like you should be able to create new objects while creating new objects.

It’s possible that the code you showed isn’t where the problem is occurring. See, it complains about using the stack and that implies that there is a variable declared which isn’t a pointer. Like, if you had

void foo()
{
  AMyActor myActor;
}

…it would complain because you have to create actors dynamically. So is there anywhere that you may be doing that?

Two years into using UE4 and I still feel like an idiot…

Wow thanks guys, that’s a lot of usefull information!

First of all, yep I’m dumb and tried to use NewObject() during the construction process of my PlayerCharacter.

Changing it to CreateDefaultSubobject<>() doesn’t do the trick, though.

I’m going to post the functions that seem to cause the hickup right here:

The one you allready know:

void URotationAnatomyMap::SetupNewComponent(FName InComponentName, float InPositiveYaw, float InNegativeYaw, float InPositivePitch, float InNegativePitch, float InPositiveRoll, float InNegativeRoll)
{
	URotationAnatomyMapComponent* TempMapComponent = NewObject<URotationAnatomyMapComponent>(this);		// This line was mentioned in the debug info, changed it still didn't work
	TempMapComponent->SetRotationValues(InPositiveYaw, InNegativeYaw, InPositivePitch, InNegativePitch, InPositiveRoll, InNegativeRoll);
	RotationAnatomyMapComponents.Add(InComponentName, TempMapComponent);
}

And the constructor of my PlayerCharacter:

APlayerCharacter::APlayerCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	// Setting up the main camera
	FPCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FPCamera"));
	FName HeadSocket = FName(TEXT("head"));
	FPCamera->AttachTo(GetMesh(), HeadSocket);

	// Setting up the rotation anatomy map
	RotationLimits = CreateDefaultSubobject<URotationAnatomyMap>(TEXT("RotationLimits"));

	/* Setting up an rotation anatomy map component for the angles between camera and head.
	 * Values for Yaw and Pitch can be found here (source in german) [https://de.wikipedia.org/wiki/Augenbewegung#Leistungen_des_Bewegungsapparates]. Roll is just a guess...
	 */
	RotationLimits->SetupNewComponent(FName(TEXT("CameraHead")), 50.0f, 50.0f, 45.0f, 60.0f, 15.0f, 15.0f); 	 // This line was mentioned in the debug info
}

Let me know if you need additional info!

Afaik the stack is where he stores stuff that it is “statically” defined in the code like:

FVector X = Fvector(5.0f, 5.0f, 5.0f);
int32 B = 5;

Usually in c++ almost everything that was a pointer was something allocated in the heap with the new command, so taking into account that most of what you work in terms of uobjects (that always need to use the CreateDefaultSubobject or the NewObject) are with pointers, my guess is that most of it is in the heap. I have more than 3 years with UE 4 and am always learning new things, so don’t feel dumb about it, it’s a huge engine :stuck_out_tongue: You are still calling a NewObject in the constructor when calling SetupNewComponent. Try using the CreateDefaultSubobject instead. The thing is, do you really need to create the DataAsset in the constructor? If you create it in BeginPlay you should be able to use NewObject i believe.

I don’t know… Maybe just try and see if it works when you call it from BeginPlay? If that works then it’s somehow related to the constructor.

void URotationAnatomyMap::SetupNewComponent(FName InComponentName, float InPositiveYaw, float InNegativeYaw, float InPositivePitch, float InNegativePitch, float InPositiveRoll, float InNegativeRoll)
{
URotationAnatomyMapComponent* TempMapComponent = CreateDefaultSubobject(FName(TEXT(“TempMapComponent”))); // This line was mentioned in the debug info
TempMapComponent->SetRotationValues(InPositiveYaw, InNegativeYaw, InPositivePitch, InNegativePitch, InPositiveRoll, InNegativeRoll);
RotationAnatomyMapComponents.Add(InComponentName, TempMapComponent);
}

Produces the same nasty error… :frowning:

When I tried to move the funtion into the BeginPlay() routine the engine still crashed on startup, but I have noticed some stange behaviour.

The error still points me to line 28 in my PlayerCharacter.cpp file (which is currently empty…)

Could it be, that the engine deosn’t reload the code anymore? How can I enforce that?

You reload the code by restarting the engine or using hot reload, you can put a breakpoint in any function and if the breakpoint is red then you have the latest version, if it’s white it’s out of date.

The engine crashes on startup…