Crash in EmptyAndAddUninitializedValues() when compiling Blueprint

We started seeing a crash when compiling one of our more complex blueprints after upgrading to 4.17.2. It’s failing the check(Count>=0) in EmptyAndAddUninitializedValues(). This is happening when duplicating the class at the start of FBlueprintCompileReinstancer::MoveCDOToNewClass() in KismetReinstanceUtilities.cpp, inside StaticDuplicateObject.

I wasn’t able to find anything wrong in the blueprint itself. The crash happens when duplicating one of its dependent blueprints.

I’ve managed to track this back to CL 3597636 which moved the CDO clearing from before this duplication to after. Changing this back has stopped the crash for us.