【问题标题】:UPROPERTY destructed after actor construct演员构造后UPROPERTY被破坏
【发布时间】:2019-07-11 02:47:30
【问题描述】:

我无法理解问题所在。我创建了 2 个类 AMyActor 和 UObject。在 AMyActor 中,有一个 UObject 字段被标记为属性。由于某种原因,在 AMyActor 构造函数之后,垃圾收集器删除了 UObject 对象。

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

输出

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_Default
LogTemp: 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

【问题讨论】:

  • 在BeginPlay()方法中移动obj = NewObject&lt;UMyObject&gt;();是否有效?

标签: unreal-engine4


【解决方案1】:

这并不是 UE4 类的真正工作方式;这有点头疼。

UCLASS 构造函数不是在构造对象(即类的实例)时调用的函数(正如您所期望的使用“vanilla”C++ 对象)。相反,类构造函数负责构造 类默认对象 (CDO)。然后在实例化类时将 CDO 用作模板。类似地,CDO 是用于创建其他对象的类的“母盘”实例。您的 UCLASS 构造函数只被调用一次:在 CDO 上。

因此,通过调用NewObject,您创建了一个新对象并将指向该对象的指针分配给CDO 的obj 成员。然后你继续在世界上创建一个演员的实例,你的指针是空的。您的对象尚未被垃圾收集;它从一开始就没有被分配。

您需要调用NewObject 而不是CreateDefaultSubobject。这告诉 CDO 当您的 AMyActor 角色被实例化时,应该创建一个模板指定类型的新对象。 NewObject 用于在 actor 的生命周期中在 PreInitializeComponents 和 EndPlay 之间调用的动态生成的对象。

TL;DR:如果您想在构造函数中调用它,请将您的 NewObject 调用更改为 CreateDefaultSubobject。或者将 NewObject 调用移动到您的演员的 PostInitializeComponents 函数。

https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/Objects https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/Actors/ActorLifecycle

注意。请注意,提供的 Objects 文档链接已严重过时(v4.11;我注意到它引用了 ConstructObject,该链接已被弃用和删除)。然而,在撰写本文时 (4.21),它关于 CDO 的内容并没有改变。

【讨论】:

    猜你喜欢
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 2020-02-17
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多