[UE5 C++] type Cast destroys source+dest object in UE5, always worked in UE4

I’m currently preparing my plugin (Comment Bubble Assist) for UE5.
situation: iterating over an array and then performing a cast, see below

Now in UE5, after like 4 or 5 iterations, when doing the cast,
BOTH UNode and Node are suddenly NULL,
causing a crash of course, though array has like 15 elements.
The array itself is still valid, no change.

Worked flawlessly for a long time in UE4 without any problems.
I have no idea what has changed or what I’m suddenly doing wrong.
(though I must admit that casting for me is always trial-and-error :slight_smile: )
Any Idea what to change?

(...)
typedef TSet<class UObject*> FGraphPanelSelectionSet; (in UE5 GraphEditor.h)
(...)
FGraphPanelSelectionSet SelectedNodes;
(...)
SelectedNodes = GraphEditor->GetSelectedNodes();
for (UObject* UNode : SelectedNodes.Array())
{
....UK2Node* Node = Cast<UK2Node>(UNode); <<<---- sets BOTH (!) Node+Unode to NULLPTR after some iterations
....if (Node->bCommentBubbleVisible
........&& !Node->NodeComment.IsEmpty()
........&& Node->NodeComment.Compare(PRE_CLEAR_STRING) != 0)
........{
............anz++;
........}
}

Solved.
Array contained different object classes; some couldn’t be casted to UK2Node, which caused the null object.
That’s normal, but what confused me was that also the UNode variable was suddenly set to null.
I assume that was due to a compiler optimization.
Added an object check to skip those objects, and now it works.