TWeakObjectPtr invalid(?) after assignment with a valid pointer. Crashes the engine after Exception

Thanks for your reply.

Just to make things clear: this code comes from the Engine itself. I did not write this. I noticed that in later versions this code has been changed to the following:

void* UserData = Actor.UserData();
UPrimitiveComponent* PossibleOwner = FChaosUserData::Get(UserData);

if(PossibleOwner)
{
OutOverlap.Component = PossibleOwner;
OutOverlap.OverlapObjectHandle = FActorInstanceHandle(OutOverlap.Component->GetOwner());
OutOverlap.ItemIndex = INDEX_NONE;
}

I assume the casting is not the problem: if the casting does not succeed, then PossibleOwner should be nullptr, and the bold line is never executed. (Correct me if I’m wrong.)

Looking at the code of FChaosUserData::Get for primitive components, there should not be a misinterpretation in case the C-style casting did some weird stuff:

template <> FORCEINLINE UPrimitiveComponent* FChaosUserData::Get(void* UserData)
{
if (!UserData ||
((FChaosUserData*)UserData)->Type != EChaosUserDataType::PrimitiveComponent)
{
return nullptr;
}
return (UPrimitiveComponent*)((FChaosUserData*)UserData)->Payload; }

I could try upgrading the project to a newer engine version, however this does not seems a good solution and may not fix the origin of the problem.
(Also, I want to understand what’s going wrong, and more importantly, if the source IS actually in my code.)