瀏覽代碼

Reap dead observer boxes when registering with the broadcaster

Nothing ever removed deallocated observers' WeakObject boxes, so the
process-wide observer sets grew on every screen open/close. Compact the
set on every add — re-registration reaps the boxes previous instances
left behind.
Marvin Polscheit 3 天之前
父節點
當前提交
8ac5b26a7d
共有 1 個文件被更改,包括 8 次插入0 次删除
  1. 8 0
      Trio/Sources/Services/Notifications/SwiftNotificationCenter/WeakObjectSet.swift

+ 8 - 0
Trio/Sources/Services/Notifications/SwiftNotificationCenter/WeakObjectSet.swift

@@ -41,6 +41,9 @@ struct WeakObjectSet<T: AnyObject>: Sequence {
     }
     }
 
 
     mutating func add(_ object: T) {
     mutating func add(_ object: T) {
+        // Registration doubles as the reaping hook — dead boxes are removed nowhere
+        // else, and most observers never unregister explicitly.
+        compact()
         // prevent ObjectIdentifier be reused
         // prevent ObjectIdentifier be reused
         if contains(object) {
         if contains(object) {
             remove(object)
             remove(object)
@@ -48,6 +51,11 @@ struct WeakObjectSet<T: AnyObject>: Sequence {
         objects.insert(WeakObject(object))
         objects.insert(WeakObject(object))
     }
     }
 
 
+    /// Drops entries whose weakly-held object has been deallocated.
+    mutating func compact() {
+        objects = objects.filter { $0.object != nil }
+    }
+
     mutating func add(_ objects: [T]) {
     mutating func add(_ objects: [T]) {
         objects.forEach { self.add($0) }
         objects.forEach { self.add($0) }
     }
     }