@@ -1348,6 +1348,21 @@ protected NodeModel()
13481348 OutPorts . CollectionChanged += PortsCollectionChanged ;
13491349 }
13501350
1351+ private void DisposePort ( PortModel portModel , bool nodeDisposing = false )
1352+ {
1353+ portModel . PropertyChanged -= OnPortPropertyChanged ;
1354+ portModel . ConnectorCollectionChanged -= ConnectorsCollectionChanged ;
1355+
1356+ // if this node is being disposed, we don't need to set node state and destroy the connectors,
1357+ // as the connectors will be deleted elsewhere
1358+ if ( ! nodeDisposing )
1359+ {
1360+ portModel . DestroyConnectors ( ) ;
1361+ SetNodeStateBasedOnConnectionAndDefaults ( ) ;
1362+ }
1363+ portModel . Dispose ( ) ;
1364+ }
1365+
13511366 private void PortsCollectionChanged ( object sender , System . Collections . Specialized . NotifyCollectionChangedEventArgs e )
13521367 {
13531368 switch ( e . Action )
@@ -1356,25 +1371,15 @@ private void PortsCollectionChanged(object sender, System.Collections.Specialize
13561371 ConfigureSnapEdges ( sender == InPorts ? InPorts : OutPorts ) ;
13571372 foreach ( PortModel p in e . NewItems )
13581373 {
1359- p . Connectors . CollectionChanged += ( coll , args ) =>
1360- {
1361- // Call the collection changed handler, replacing
1362- // the 'sender' with the port, which is required
1363- // for the disconnect operations.
1364- ConnectorsCollectionChanged ( p , args ) ;
1365- } ;
13661374 p . PropertyChanged += OnPortPropertyChanged ;
1375+ p . ConnectorCollectionChanged += ConnectorsCollectionChanged ;
13671376 SetNodeStateBasedOnConnectionAndDefaults ( ) ;
13681377 }
13691378 break ;
13701379 case System . Collections . Specialized . NotifyCollectionChangedAction . Remove :
13711380 foreach ( PortModel p in e . OldItems )
13721381 {
1373- p . PropertyChanged -= OnPortPropertyChanged ;
1374-
1375- p . DestroyConnectors ( ) ;
1376-
1377- SetNodeStateBasedOnConnectionAndDefaults ( ) ;
1382+ DisposePort ( p ) ;
13781383 }
13791384 break ;
13801385 }
@@ -2929,6 +2934,26 @@ private void OnRenderPackagesUpdated(RenderPackageCache packages)
29292934 RenderPackagesUpdated ( this , packages ) ;
29302935 }
29312936 }
2937+
2938+
2939+ public override void Dispose ( )
2940+ {
2941+ if ( HasBeenDisposed ) return ;
2942+
2943+ base . Dispose ( ) ;
2944+
2945+ InPorts . CollectionChanged -= PortsCollectionChanged ;
2946+ foreach ( var port in InPorts )
2947+ {
2948+ DisposePort ( port , nodeDisposing : true ) ;
2949+ }
2950+
2951+ OutPorts . CollectionChanged -= PortsCollectionChanged ;
2952+ foreach ( var port in OutPorts )
2953+ {
2954+ DisposePort ( port , nodeDisposing : true ) ;
2955+ }
2956+ }
29322957 }
29332958
29342959 /// <summary>
0 commit comments