x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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
Product Version: UE 4.13
Tags:
more ▼

asked Sep 24 '16 at 06:18 PM in C++ Programming

avatar image

NikDouglas
18 1 4 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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);
 
     }
more ▼

answered Sep 24 '16 at 07:30 PM

avatar image

GigasightMedia
1.7k 43 12 61

avatar image NikDouglas Sep 24 '16 at 08:58 PM

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

avatar image NikDouglas Sep 25 '16 at 08:34 AM

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

avatar image GigasightMedia Sep 25 '16 at 05:23 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question