I can not understand what the problem is. I created 2 classes AMyActor and UObject. In AMyActor, there is a UObject field marked as a property. For some reason, after the AMyActor constructor, the garbage collector deletes the UObject object.
MyActor.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "MyObject.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class MYPROJECT2_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY()
UObject *obj;
UPROPERTY(EditAnywhere)
UStaticMeshComponent *mesh;
};
MyActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
obj = NewObject<UMyObject>();
mesh = CreateDefaultSubobject<UStaticMeshComponent>("ms");
UE_LOG(LogTemp, Warning, TEXT("actor create %d %d"), this, obj);
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
UE_LOG(LogTemp, Warning, TEXT("actor %d %d"), this, obj);
}
MyObject.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "MyObject.generated.h"
/**
*
*/
UCLASS()
class MYPROJECT2_API UMyObject : public UObject
{
GENERATED_BODY()
UMyObject()
{
UE_LOG(LogTemp, Warning, TEXT("object create %d"), this);
}
~UMyObject()
{
UE_LOG(LogTemp, Warning, TEXT("object destroy %d"), this);
}
};
Output
PIE: New page: PIE session:
Minimal_Default (Feb 17, 2019, 7:48:39
PM) LogPlayLevel: Creating play world
package:
/Game/StarterContent/Maps/UEDPIE_0_Minimal_DefaultLogTemp: Warning: object create 1801824896
LogTemp: Warning: actor create 1441518592 1801824896
LogPlayLevel: PIE:
StaticDuplicateObject took:
(0.005484s) LogAIModule: Creating
AISystem for world Minimal_Default
LogPlayLevel: PIE: World Init took:
(0.001288s) LogPlayLevel: PIE: Created
PIE world by copying editor world from
/Game/StarterContent/Maps/Minimal_Default.Minimal_Default
to
/Game/StarterContent/Maps/UEDPIE_0_Minimal_Default.Minimal_Default
(0.007418s)
LogTemp: Warning: object destroy 1801824896
LogInit: XAudio2
using ‘Speakers (Realtek High
Definition Audio(SST))’ : 2 channels
at 48 kHz using 32 bits per sample
(channel mask 0x3) LogInit:
FAudioDevice initialized.
LogUObjectGlobals: Warning: Failed to
find object ‘Class
/Script/MyProject2.MyProject2GameMode’
LogLoad: Game class is ‘GameModeBase’
LogWorld: Bringing World
/Game/StarterContent/Maps/UEDPIE_0_Minimal_Default.Minimal_Default
up for play (max tick rate 60) at
2019.02.17-17.48.39 LogWorld: Bringing up level for play took: 0.002244
LogContentBrowser: Native class
hierarchy updated for
‘MovieSceneCapture’ in 0.0004 seconds.
Added 10 classes and 0 folders. PIE:
Play in editor start time for
/Game/StarterContent/Maps/UEDPIE_0_Minimal_Default
0.179 LogBlueprintUserMessages: Late PlayInEditor Detection: Level
‘/Game/StarterContent/Maps/Minimal_Default.Minimal_Default:PersistentLevel’
has LevelScriptBlueprint
‘/Game/StarterContent/Maps/Minimal_Default.Minimal_Default:PersistentLevel.Minimal_Default’
with GeneratedClass
‘/Game/StarterContent/Maps/Minimal_Default.Minimal_Default_C’
with ClassGeneratedBy ‘/
Game/StarterContent/Maps/Minimal_Default.Minimal_Default:PersistentLevel.Minimal_Default’LogTemp: Warning: actor 1441518592 0
LogTemp: Warning: actor 1441518592 0
LogTemp: Warning: actor 1441518592 0
LogTemp: Warning: actor 1441518592 0
LogTemp: Warning: actor 1441518592 0
LogTemp: Warning: actor 1441518592 0
LogTemp: Warning: actor 1441518592 0