C++ Lambda Delegates crashing in packaged build

Hi,
I have an issue with my implementation of lambda delegates in UE4. It works perfectly in the editor but as soon as I use it in a packaged build, it gives a fatal error with no meaningful info.

This is the code being used. It essentially wraps a more meaningful delegate around another delegate.

FDelegateHandle UReiOnline::ReadUserFile_Internal(const int32 UserNum, const FString& FileName,
	IOnlineIdentityPtr Identity, IOnlineUserCloudPtr UserCloud,
	const FReiOnReadUserFileCompleteDelegate& FileCompleteDelegate)
{
	const TSharedPtr<FDelegateHandle> NativeHandle(new FDelegateHandle());
	const FOnReadUserFileCompleteDelegate NativeHandleDel = FOnReadUserFileCompleteDelegate::CreateLambda([UserCloud, FileName, UserNum](
	bool Success, const FUniqueNetId& UniqueNetId, const FString& ThisFileName, FReiOnReadUserFileCompleteDelegate Delegate, TSharedPtr<FDelegateHandle> NativeHandle)
	{
		// Ignore if it isn't this file.
		if (!ThisFileName.Equals(FileName))
			return;

		// Unsubscribe.
		UserCloud->ClearOnReadUserFileCompleteDelegate_Handle(*NativeHandle);
		NativeHandle->Reset();
		
		const FString Contents = GetCachedUserFileContents(UserNum, ThisFileName);
		Delegate.ExecuteIfBound(Success, UniqueNetId, ThisFileName, Contents);
	}, FileCompleteDelegate, NativeHandle);

	// Get reference to the delegate handle so it can be unsubscribed to later.
	*NativeHandle = UserCloud->AddOnReadUserFileCompleteDelegate_Handle(NativeHandleDel);

	const FUniqueNetId& UserNetId = *Identity->GetUniquePlayerId(UserNum).Get();
	
	// Read failed
	if (!UserCloud->ReadUserFile(UserNetId, FileName))
	{
		FileCompleteDelegate.ExecuteIfBound(false, UserNetId, FileName, FString());
		return FDelegateHandle();
	}
	
	return FileCompleteDelegate.GetHandle();
}

I understand that the code is a bit trippy since it’s kinda going back on itself but I was unsure on another way to implement this behaviour. The ReadUserFile function doesn’t include any info about the contents of the file, so this was my way around it.

I’ve tried changing const TSharedPtr<FDelegateHandle> NativeHandle(new FDelegateHandle()); to const TSharedPtr<FDelegateHandle> NativeHandle(MakeShareable(new FDelegateHandle())); as well as using normal C pointers and using the delete keyword, but the result is the same.

I have also implemented similar behaviour for Writing User Files, where the function automatically converts an FString to a UTF8 array (which is required for OnlineUserCloudInterface::WriteUserFile)

Am I just being dumb and is there an easier way to implement this behaviour?

Okay this came to a surprise to me but it looks like this specific line was the cause of the crashes: const FUniqueNetId& UserNetId = *Identity->GetUniquePlayerId(UserNum).Get();.

I only realised because I refactored a little bit to replace int UserNum to FUniqueNetId& since it’s required to get non-local client data anyways. The UserNum->NetId conversion is done earlier on in the process now (if not using NetIds directly).