TObjectPtr uninitialized?!

I’ve got a function that returns TObjectPtr. Now, my understanding of TObjectPtr (or one of the benefits at least) is that it guarantees nullptr initialization. The problem I’m having is that I can’t get the following to compile

TObjectPtr<AMyActor> tmpActor;
return tmpActor;

That gives me a compiler error

uninitialized local variable 'tmpActor' used

Is this just a case where the compiler can’t “see that deep” and doesn’t know that the template initializes? Or am I just thinking about this wrong?

I made a similar example and it compiled, obviously I used AActor instead, do you get the same issue if you use AActor as well ?

image

1 Like

Good question, let me give that a shot…

Your example worked but try this…

TObjectPtr<AActor> ACodeFiveCharacter::GetActorPtr() {
	AActor* teamSet = nullptr;
	TObjectPtr<AActor> spawnedPiece;
	TArray<TObjectPtr<AActor>> spawnedPieces;

	if (teamSet == nullptr) {
		return spawnedPiece;
	}

	return spawnedPiece;

}

That is the most compact example that gives the unitialized compiler error for me.

What’s interesting is both of these compile just fine

TObjectPtr<AActor> ACodeFiveCharacter::GetActorPtr() {
	//FChessSet* teamSet = nullptr;
	TObjectPtr<AActor> spawnedPiece;
	TArray<TObjectPtr<AActor>> spawnedPieces;

	//if (teamSet == nullptr) {
	//	return spawnedPiece;
	//}

	return spawnedPiece;

}
TObjectPtr<AActor> ACodeFiveCharacter::GetActorPtr() {
	FChessSet* teamSet = nullptr;
	TObjectPtr<AActor> spawnedPiece;
	//TArray<TObjectPtr<AActor>> spawnedPieces;

	if (teamSet == nullptr) {
		return spawnedPiece;
	}

	return spawnedPiece;

}

Do you see the same results?

Interesting, yes I got the same results.
If I add this beforehand it does compile.
image

This example is probably not the best so I’m sure if the code is adjusted it should work.

Appreciate the verification/validation. I went ahead and simply initialized it to nullptr for this instance. I suspect there’s just something off with the order of operations here such that the compiler loses it’s mind. Just surprised I haven’t run into this until now. TObjectPtr does in fact initialize to nullptr as advertised.

1 Like