From d473ab9d223dfdb00938b32468c8f9f42cb0b19e Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Thu, 26 Oct 2023 09:48:56 -0700 Subject: [PATCH] Fix retain loop for enrichment plugin closure. (#262) --- Sources/Segment/Plugins.swift | 2 +- Tests/Segment-Tests/MemoryLeak_Tests.swift | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/Segment/Plugins.swift b/Sources/Segment/Plugins.swift index 09b19dc9..7890e77a 100644 --- a/Sources/Segment/Plugins.swift +++ b/Sources/Segment/Plugins.swift @@ -68,7 +68,7 @@ internal protocol PlatformPlugin: Plugin { } public typealias EnrichmentClosure = (_ event: RawEvent?) -> RawEvent? public class ClosureEnrichment: Plugin { public var type: PluginType = .enrichment - public var analytics: Analytics? = nil + public weak var analytics: Analytics? = nil internal let closure: EnrichmentClosure diff --git a/Tests/Segment-Tests/MemoryLeak_Tests.swift b/Tests/Segment-Tests/MemoryLeak_Tests.swift index 52ce0f67..bfb2e125 100644 --- a/Tests/Segment-Tests/MemoryLeak_Tests.swift +++ b/Tests/Segment-Tests/MemoryLeak_Tests.swift @@ -45,6 +45,11 @@ final class MemoryLeak_Tests: XCTestCase { let macLifecycle = analytics.find(pluginType: macOSLifecycleEvents.self)! let macMonitor = analytics.find(pluginType: macOSLifecycleMonitor.self)! #endif + + // test that enrichment closure isn't leaked. was previously a retain loop. + analytics.add { event in + return event + } analytics.remove(plugin: startupQueue) analytics.remove(plugin: segmentDest)