Download

Crash when accessing member varible

Hello,

I have a class called ABuildingManager, which is derived from AActor.

If I try to access member variables of this class, even inside itself, it crashes the editor.

I have created an ABuildingManager object inside my PlayerController class like this:



BuildingManager = NewObject<ABuildingManager>();


Inside BuildingManager.h i have



int32 a = 5


and in my ABuildingManager::SpawnObject() method I do



a = a + a;


Obviously a = a + a is some nonsense code but it still crashes. If I access a member variable of ABuildingManager i get crash no matter what. I have never experienced anything like this before. What am I missing here?

Edit:
Spawning in world fixed the problem. I have no idea why.



if( UWorld* World = GetWorld() )    
{        
    BuildingManager = World->SpawnActor<ABuildingManager>( ABuildingManager::StaticClass(), FTransform::Identity );    
}


Hey Metalwrath, are you getting a null pointer error? I had a crashing project I panicked about for a bit before realizing it was a null pointer error. Can you paste the stack trace?

Hello, its not a null pointer error. It either does not print anything or says unhandled exception. I can’t post right now as im not on my computer but I will post it tomorrow.

Oh, that looks complex to solve, but I hope to get some definite answer from experts here.

I assume BuildingManager is a member variable inside your PlayerController?
If it’s a raw pointer (ABuildingManager* BuildingManager) make sure it’s marked as UPROPERTY(), otherwise it will be garbage collected.

Secondly, I would suggest you use the SpawnActor-function instead of NewObject.
Although I’m successfully able to use NewObject on actors on my end without any crash, I’ve personally never done it.
Try replacing the NewObject call with


    if( UWorld* World = GetWorld() )
    {
        BuildingManager = World->SpawnActor<ABuildingManager>( ABuildingManager::StaticClass, FTransform::Identity );
    }

Here it is:



Unhandled exception

UE4Editor_AgileGame_0154!ABuildingManager::SpawnObject() [E:\AgileDev\Source\AgileGame\ItemPlacement\BuildingManager.cpp:93]
UE4Editor_AgileGame_0154!AAgileGamePlayerController::PlaceObject() [E:\AgileDev\Source\AgileGame\AgileGamePlayerController.cpp:174]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll


I have tried UPROPERTY() macro but nothing changed :frowning:

Yes It is a member in PlayerController. There is no problem when I access BuildingManager through player controller. But I get a crash if I access a variable of BuildingManager, even from inside BuildingManager itself.

Thing is I don’t really want to spawn it in world. In other projects I always created objects in code without spawning in world (they don’t need to be in world in my game) and had no problems. It is really weird because i am doing exactly like I did before but i am getting crash for even accessing a member variable of BuildingManager class. Either I miss something obvious or something in my game bugged out.

Edit: Spawning in world fixed the problem. I have no idea why.

If you don’t want to spawn it in world - don’t make it an Actor. If you only require an object containing calculations, I recommend you don’t make it an Actor, but a UObject. An Actor is specifically for objects requiring an in-world representation. I recommend you read up on Unreal’s class hierarchies and what each class is used for :slight_smile:

It was originally an UComponent, I changed it to Actor to test if that was the reason. But yeah you are correct.