Crash after a while when constantly reading variable

VERSION 4.17.2

Hey guys,
I think I might have found an Engine bug but as I’m not sure if I’m just making some dumb mistake I’ll just post it in C++ for now. It would surprise me, though, as I’ve been working with C++ in UE4 for almost two months now and it seems unlikely that I have been doing this wrong the whole time but it just started crashing now.




I was able to reproduce the problem in a complete new project so I’ll just show you with this.
Here’s the code, explanations below:


MyObject.h:

UCLASS()
class TEMP_API UMyObject : public UObject
{
  GENERATED_BODY()

public:
    FString str;
};

MyActor.h:

UCLASS()
class TEMP_API AMyActor : public AActor
{
	GENERATED_BODY()
	
public:	
	AMyActor();

protected:
	virtual void BeginPlay() override;

public:	
	virtual void Tick(float DeltaTime) override;

private:
  UMyObject* myObject;
};

MyActor.cpp:

AMyActor::AMyActor()
{
	PrimaryActorTick.bCanEverTick = true;
}

void AMyActor::BeginPlay()
{
  myObject = NewObject<UMyObject>();
  myObject->str = "test";

	Super::BeginPlay();
}

void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
  FString test = myObject->str;
}

Now this is as simple as it gets.

The only thing I do is creating an instance of the UMyObject class and initializing its “str” member with “test”.
Then I proceed to read “str” with every tick.

I created a blueprint instance of MyActor, placed it in the game world and started it. Nothing else!


And after a while the game crashes with Reading violation 0xFFFF… error.

I’ve been sitting over this for hours and I’m really out of ideas.
Has this ever happened to anyone before?

Can anyone else reproduce it?

Is it an Engine bug?
Unfortunately I can’t really test it on other versions right now

Sounds like the garbage collection is destroying your object so the pointer is no longer valid.

See if this helps: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

I didn’t even know UE use a garbage collector…
But anyways, it says that “UE4 Garbage Collection only counts references to UObjects that are UPROPERTY()” which is not the case here so it shouldn’t actually be garbage collected, right?
EDIT:
Nevermind…I should read the whole thing

EDIT2: This actually seems to do the trick, thanks a lot!

Try calling Super::BeginPlay() before the object initialization and assignment and add this in for good measure as a check to see if the object is null or not. You never know. :stuck_out_tongue:

if(myObject != NULL)
{
  GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::White, FString::FString("Object is not null!"), true);
}

else
{
   GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::White, FString::FString("Object is null!"), true);
}

The problem is that in this case the variable isn’t actually “NULL” but “Invalid” and even if it was NULL, checking it would not solve my problem of it suddenly getting deleted.
Anyhow, Jin_VE solved it but thanks for the answer