Object References Vanishing

Any time I attempt to instantiate an object via C++ using both NewObject() (for UObject types) and CreateDefaultSubobject() (for ActorComponent types), the reference will become invalid as soon as I’ve left the scope of the function. This occurs regardless of which function I call it in. I’ve tried constructors/BeginPlay/a custom initialization function. In all cases, the member variable I keep attempting to store it in is declared as a UPROPERTY and is simply a pointer to the type. Nevertheless, the variable will cease to reference the instanced object. I can tell that the instanced object continues to exist because tick events will still trigger, but no references of mine will reach it. Can anyone explain what might be happening here?

Can you show the code? I don’t think it’s GC, if it was it would clean it up in specific static number of seconds

My default pawn in “public:”

// The SkillUserComponent that manages the stats of the user
    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Properties")
    USkillUserComponent *SkillUserComponent;

the pawn printing to the screen…

// Called when the game starts or when spawned
    void ASkillUsingPawn::BeginPlay()
    {
        Super::BeginPlay();
        if (SkillUserComponent) {
            print("I have a SkillUserComponent");
            ASkillManager *sm = nullptr;
            if (IsValid(sm = Cast<AMyGameMode>(()->GetAuthGameMode())->SkillManager)) {
                print("A SkillManager exists within the GameMode!");
                if (IsValid(sm->MasterArsenal)) {
                    print("MasterArsenal is valid. Assigning to SkillUser");
                    SkillUserComponent->SkillSystem->Arsenal = sm->MasterArsenal;
                }
            }
        }
    }

The SkillUserComponent was being initialized in the ASkillUsingPawn constructor via…

SkillUserComponent = CreateDefaultSubobject<USkillUserComponent>(TEXT("SkillUserComponent"));
    SkillUserComponent->RegisterComponent();

…and the reference remains valid here, but by the BeginPlay event, it doesn’t print anything. When I attached the components in a Blueprint and assigned them to the member variables in the ConstructionScript, THEN it worked.

Same w/ SkillManager, the GameMode.h has the following declaration…

public:

    // The SkillManager that manages the application of skill mods/effects/combat/etc. (Should only be 1)
    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SkillManager")
    ASkillManager *SkillManager;

PlayerController

void ASkillUserPlayerController::BeginPlay() {
    ASkillManager *sm = ()->SpawnActor<ASkillManager>(FVector(0.0f), FRotator(0.0f));
    AMyGameMode *gm = Cast<AMyGameMode>(()->GetAuthGameMode());
    gm->SkillManager = sm;
}

This, however, doesn’t result in the SkillManager “exists” print statement triggering. What does is spawning one via Blueprints in the playercontroller beginplay event and assigning it to the var.

try adding this after CreateDefaultSubobject

AddComponent(FName("SkillUserComponent"), false, FTransform(), NULL);

Also check if component is visible in blueprint inhereted from that Pawn or check in details tab when you select Pawn.

I just tried doing this in a new project with a c++ player controller spawning a c++ actor which has CreateDefaultSubobject of a c++ actor component in its constructor. The component then has a c++ uobject (created using NewObject) with an integer value. My goal is to be able to access the integer value after spawning the actor. I can’t even manage to spawn the actor though.
link text

I would certainly try to do this, but I can’t even get the actor to spawn in the first place. If the actor could be generated, then I could check to see if the component is generated properly.

Later, I should be able to spawn the actor via Blueprints and then see if the component stuff works, but I can’t at the moment. I would prefer to figure out how to spawn an actor from c++ first though, since that’s a bit more crucial.

Just calling ()->SpawnActor(…) in the overridden BeginPlay event of a player controller doesn’t seem to do the trick.

Found out I’m an idiot. I eventually realized that I had somehow accidentally switched the build type of my VS editor to be Win32 instead of Win64 in my main project, so I was able to get things working in a separate project, but not in my main one. Question should be closed now.

It would appear that changing back to Win64 build still hasn’t fixed the issue. I now have a working version in a basic form in a new project so that I have an actor spawned from the game mode’s begin play function that contains an instanced component with an instanced UObject with an int32 that I can access successfully. Running the same scenario in my actual project doesn’t work.

In my actual project, I can’t spawn an actor from my game mode’s begin play, but I can spawn one from my player controller’s begin play. That actor then receives no components unless they are attached via a Blueprint Components panel in a derived Blueprint class. Even then, the UObject instances inside the components are never successfully maintained.

So, to be clear, the functions I call give me an instance for both CreateDefaultSubobject and NewObject, but the references don’t last (at least, I can confirm that components’ tick event still triggers). I attempted 's suggestion, but apparently the AddComponent function is deprecated and is only used internally by the Blueprint system itself now. So that doesn’t work.