@@ -451,23 +451,27 @@ impl Graph {
451
451
// Nodes are only dropped when they do not have incoming connections.
452
452
// But they may have AudioParams feeding into them, these can de dropped too.
453
453
nodes. retain ( |id, node| {
454
+ let node = node. get_mut ( ) ; // unwrap the RefCell
455
+
454
456
// Check if this node was connected to the dropped node. In that case, it is
455
457
// either an AudioParam (which can be dropped), or the AudioListener that feeds
456
458
// into a PannerNode (which can be disconnected).
457
459
let was_connected = {
458
- let outgoing_edges = & mut node. borrow_mut ( ) . outgoing_edges ;
460
+ let outgoing_edges = & mut node. outgoing_edges ;
459
461
let prev_len = outgoing_edges. len ( ) ;
460
462
outgoing_edges. retain ( |e| e. other_id != * index) ;
461
463
outgoing_edges. len ( ) != prev_len
462
464
} ;
463
465
464
- // retain when special or not connected to this dropped node
465
- let special = id < 2 ; // never drop Listener and Destination node
466
- let retain = special || !was_connected;
466
+ // Retain when
467
+ // - special node (destination = id 0, listener = id 1), or
468
+ // - not connected to this dropped node, or
469
+ // - if the control thread still has a handle to it.
470
+ let retain = id < 2 || !was_connected || !node. free_when_finished ;
467
471
468
472
if !retain {
469
473
self . reclaim_id_channel
470
- . push ( node. get_mut ( ) . reclaim_id . take ( ) . unwrap ( ) ) ;
474
+ . push ( node. reclaim_id . take ( ) . unwrap ( ) ) ;
471
475
}
472
476
retain
473
477
} )
@@ -727,6 +731,8 @@ mod tests {
727
731
// Add an AudioParam at id 4, it should be dropped alongside the regular node
728
732
let param = Box :: new ( TestNode { tail_time : true } ) ; // audio params have tail time true
729
733
add_node ( & mut graph, 3 , param) ;
734
+ // Mark the node as 'detached from the control thread', so it is allowed to drop
735
+ graph. nodes [ 3 ] . get_mut ( ) . free_when_finished = true ;
730
736
731
737
// Connect the audioparam to the regular node
732
738
add_audioparam ( & mut graph, 3 , 2 ) ;
0 commit comments