Solidworks VBA ModifyDefinition causing crash on closing of assembly

When using ModifyDefinition to update a mate in an assembly, when I use the ModifyDefinition for a second time it causes solidworks to crash when i come to close the assembly that the macro ran on.

This is how I am using the ModifyDefinition:-

Dim swAssembly As SldWorks.AssemblyDoc

Set swAssembly = swModel

Set swFeat = swModel.FirstFeature
    While Not swFeat Is Nothing
       
           'If swFeat.Name = "CylFreeStroke" Then
           '    Debug.Print swFeat.GetTypeName2
           '    Debug.Print swFeat.TypeName
           'End If
           Set swSubFeat = swFeat.GetFirstSubFeature
           While Not swSubFeat Is Nothing
               If swSubFeat.Name = "CylFreeStroke" Then
                   'Dim swDistMateData As SldWorks.DistanceMateFeatureData
                   'Dim swMateData As SldWorks.MateFeatureData
                   Set swMateData = swSubFeat.GetDefinition
                   If swMateData.TypeName = SwConst.swMateType_e.swMateDISTANCE Then
                       Set swDistMateData = swMateData '= swSubFeat.GetDefinition
                       'Debug.Print swSubFeat.GetTypeName2
                       'Debug.Print "Max Dist.:- " & swDistMateData.MaximumDistance
                       'Debug.Print "Min Dist.:- " & swDistMateData.MinimumDistance
                       swDistMateData.MaximumDistance = CDbl(usrfrmCylinder.txbCylTravel.Value) * LengthCon
                       'update mate
                       'swModel.EditRebuild3
                       swSubFeat.ModifyDefinition swDistMateData, swAssembly, Nothing

 

*note:- the end of the Wend is not included

when i look to repeat the above code for another mate update then when i come to close the solidworks assembly it crashes. its completely stably until i look to close the assembly.

When I comment out the second us of ModifyDefinition the assembly is fine and there is no crash on closing. Any ideas?