[4.6.1] Crash when BlueprintCallable method returns TWeakObjectPtr

When I call a function from blueprint (UI in this case) and the function returns a TWeakObjectPtr it crashes. Replacing it with a regular pointer fixes the issue, but the compiler does not produce an error as it does with parameters.

Access violation - code c0000005 (first/second chance not available)

UE4Editor_CoreUObject + 1643774 bytes
UE4Editor_CoreUObject + 716678 bytes
UE4Editor_CoreUObject + 1616623 bytes
UE4Editor_CoreUObject + 1477214 bytes
UE4Editor_CoreUObject + 1350042 bytes
UE4Editor_CoreUObject + 1621364 bytes
UE4Editor_CoreUObject + 1477214 bytes
UE4Editor_CoreUObject + 563956 bytes
UE4Editor_CoreUObject + 1475014 bytes
UE4Editor_UMG + 141702 bytes
UE4Editor_UMG + 883535 bytes
UE4Editor_UMG + 53187 bytes
UE4Editor_UMG + 60825 bytes
UE4Editor_UMG + 541413 bytes
UE4Editor_Slate + 2116506 bytes
UE4Editor_Slate + 2296435 bytes
UE4Editor_Slate + 432060 bytes
UE4Editor_Slate + 160583 bytes
UE4Editor_Slate + 832224 bytes
UE4Editor_Slate + 770598 bytes
UE4Editor_Core + 2484713 bytes
UE4Editor_Core + 2400763 bytes
UE4Editor_Core + 2489275 bytes
UE4Editor_Core + 2387186 bytes
user32 + 105425 bytes
user32 + 104666 bytes
UE4Editor_Core + 2490486 bytes
UE4Editor!FEngineLoop::Tick() + 3434 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.6\engine\source\runtime\launch\private\launchengineloop.cpp:2193]
UE4Editor!GuardedMain() + 479 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.6\engine\source\runtime\launch\private\launch.cpp:131]
UE4Editor!GuardedMainWrapper() + 26 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.6\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() + 249 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.6\engine\source\runtime\launch\private\windows\launchwindows.cpp:202]
UE4Editor!__tmainCRTStartup() + 329 bytes [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c:618]

Debugger spits this out as well:

First-chance exception at 0x000007FEE74A14FE (UE4Editor-CoreUObject.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x000000010000000B.
Unhandled exception at 0x000007FEE74A14FE (UE4Editor-CoreUObject.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x000000010000000B.

greetings,

FTC

Hey FTC,

Does this crash also occur in the 4.7 preview?

Hey Jonathan,

yes it does (I am on preview 8). This is my quick and dirty test function:

UFUNCTION(BlueprintCallable, Category="test")
TWeakObjectPtr<AActor> Bla();


TWeakObjectPtr<AActor> AMyTestActor::Bla()
{
	auto Actor = GetWorld()->SpawnActor<AStaticMeshActor>();
	return TWeakObjectPtr<AActor>(Actor);
}

It also crashes if the return value is not newly spawned, but instead a property of AMyTestActor.

greetings

FTC

Hey FTC-

Does this crash still occur for you in the latest engine version (4.7.2)? I’m trying to test the code you posted above, I created a class based on Actor and added your code to the class but I’m receiving compile errors. Did you add your test function to an actor class or another class? Let me know if there’s other steps that I’m missing.

Yes the crash still occurs and I get no compile errors with the latest launcher version. I use an actor and I have not done anything special to it. My test simply iterated through actors of that class and called the function in the level blueprint.

Here is the callstack

Hey FTC-

Rather than returning a TWeakObjectPtr you could return UObject* instead. The following segment of code is an example of how you can use a TWeakObjectPtr inside a function and then return the UObject.

UObject* Function()
{
     TWeakObjectPtr* Variable;
     return Variable.Get();
}

Cheers

Thank you, but I know I can simply return an object pointer, the whole point of it was to not do it. Using TWeakObjectPtr as parameters throws up compile errors as it is obviously not supported, returning them, however, doesn’t but it nevertheless seems to be unsupported by blueprints.

Hey FTC-

Anytime a TWeakObjectPtr could be returned, a UObject could be returned instead however I have reported the bug that attempting to return a TWeakObjectPtr does not give any error warning (UE-12214).