AccessingProperty of Instance of Standard Class Causes Editor Crash

Hi All,

I am getting an editor crash when trying to instantiate a custom (empty) C++ class and access one of its public methods.

Here is where the problem seems to occur:

    //GameState Header code
    #pragma once
    
    #include "GameFramework/GameState.h"
    #include "Engine.h"
    #include "GameStateSnapshot.h"
    #include "MyGameState.generated.h"
    
    /**
     * 
     */
    UCLASS()
    class MYGAME_API AMyGameState : public AGameState
    {
    	GENERATED_BODY()
    	
    public:
    
    	AMyGameState();
    
    	UFUNCTION()
    	void SaveSnapshot(class ACheckpoint* CheckpointToSet);
    	
    private:
    
    	GameStateSnapshot* CurrentSnapshot;
    
    
    //GameState CPP code
    
    void AMyGameState::SaveSnapshot(class ACheckpoint* CheckpointToSet)
    {
    //This seems to be causing the problem
    	CurrentSnapshot = new GameStateSnapshot;
    	CurrentSnapshot->SetCheckpoint(CheckpointToSet);
    
    }
    
    
    
    //GameStateSnapshot Header code
    #pragma once
    
    //#include "Checkpoint.h"
    
    /**
     * 
     */
    class MYGAME_API GameStateSnapshot
    {
    public:
    	GameStateSnapshot();
    	~GameStateSnapshot();
    
    
    		void SetCheckpoint(class ACheckpoint* CheckpointToSet);
    
    
    private:
    
    	class ACheckpoint* SnapshotCheckpoint;
    };
    
    //GameStateSnapshot CPP code
    
    #include "MyGame.h"
    #include "Checkpoint.h"
    #include "GameStateSnapshot.h"
    
    GameStateSnapshot::GameStateSnapshot()
    {
    }
    
    GameStateSnapshot::~GameStateSnapshot()
    {
    }
    
    void GameStateSnapshot::SetCheckpoint(class ACheckpoint* CheckpointToSet)
    {
    	SnapshotCheckpoint = CheckpointToSet;
    
    }
    
    
    
    
    //END


In my checkpoint class I have an OnBeginOverlap Event triggering a method that saves a snapshot and passes through the triggered Checkpoint as a parameter when the checkpoint is overlapped by the player. This is when the Editor crashes.

If I had any hair left I'd be pulling it out. Any help would be greatly appreciated!

-Nik

It’s probably a nullptr exception caused by the GameStateSnapshot not instantiating correctly.
Always include a null pointer check before using an unknown pointer.

if (CurrentSnapshot) {
    CurrentSnapshot->SetCheckpoint(CheckpointToSet);

}

Also, you should probably not be creating your object via “new”; you want to use NewObject. Otherwise you lose all benefit of UE4 gc and can easily introduce memory leaks (like when you don’t nullify your SnapshotCheckpoint pointer in your destructor).

For NewObject to work, you’ll need to inherit from UObject when defining your GameStateSnapshot (which then becomes UGameStateSnapshot) Is there a reason you’re not deriving from UObject (or AActor) to define your snapshot class?

    CurrentSnapshot = NewObject<UGameStateSnapshot>();
    if (CurrentSnapshot != nullptr) {
        CurrentSnapshot->SetCheckpoint(CheckpointToSet);

    }

Thanks for the reply. Only reason for not extending UObject or AActor was to keep the class lightweight as it will only act as a host for a few properties. I will first give the null pointer check a go and if no dice, use a UObject derived class. Thanks again! I will mark this as an answer as soon as I determine that it has worked.

-Nik

I tried a few permutations of your suggested solution and couldn’t eliminate the crash. I am now using a UObject and instantiating using the NewObject method with a nullptr check. Here’s what I have now:
//HEADER FILE

#pragma once

#include "GameFramework/GameState.h"
#include "Engine.h"
#include "StateSnapshot.h"
#include "MyGameState.generated.h"

/**
 * 
 */
UCLASS()
class MYGAME_API AMyGameState : public AGameState
{
	GENERATED_BODY()
	
public:

	AMyGameState();

	UFUNCTION()
	void SaveSnapshot(class ACheckpoint* SetCheckpoint);
	
private:

	UStateSnapshot* CurrentSnapshot;

};

//SOURCE FILE

#include "MyGame.h"
#include "MyGameState.h"


AMyGameState::AMyGameState()
{
	
}


void AMyGameState::SaveSnapshot(class ACheckpoint* SetCheckpoint)
{

	UStateSnapshot* SomeSnap = NewObject<UStateSnapshot>();
	if (SomeSnap != nullptr)
	{
		SomeSnap->SetCheckpoint(SetCheckpoint);
            CurrentSnapshot = SomeSnap; // <- This is the line causing the crash
	}
		
	
}

Is this error caused by me trying to reference the same memory location with two different pointers?

I want the CurrentSnapshot variable to always hold a reference to the most recently captured snapshot.

Many thanks again!

-Nik

If you want to use a standard class you can, just be sure you manage your memory if you do. In my experience the overhead costs don’t outweigh the benefits for what I’ve done so far, but your mileage may vary.

Accessing the same address with two different pointers shouldn’t be a problem.

Are you getting an error message during the crash?

Are you getting any build warnings? Is this crash occurring during PIE?

I’m assuming you’ve tracking it down to that line by commenting it out and not having a crash; am I correct in that?

Can you post the contents of your log and callstack? (~projectdir~/Saved/Logs/~yourgame~.log

Looking at what you’ve posted so far the only thing I can think to try would be making the CurrentSnapshot variable protected, rather than private; but I don’t think that’s your problem.

It might be related to not having a UPROPERTY macro set to it, but again, I don’t think that’s it. Might be worth a shot though.

Maybe try initializing the CurrentSnapshot to NULL in the constructor?

Do you mind posting the full contents of both headers and source files as they currently exist. It could be I introduced more errors.