Skip to content

Commit

Permalink
Fix leak detection and warning (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
MontakOleg authored May 31, 2024
1 parent f5c25c5 commit fccc98a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Sources/DeallocTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ fileprivate final class DeallocTracker {
/// - owner: Owner to track.
/// - closure: Closure to execute.
internal func onDealloc(of owner: Any, closure: @escaping () -> Void) {
var tracker = DeallocTracker(onDealloc: closure)
withUnsafePointer(to: tracker) { pointer in
var tracker = pointer
objc_setAssociatedObject(owner, &tracker, tracker, .OBJC_ASSOCIATION_RETAIN)
}
let tracker = DeallocTracker(onDealloc: closure)
objc_setAssociatedObject(owner, getID(tracker), tracker, .OBJC_ASSOCIATION_RETAIN)
}

// https://github.com/atrick/swift-evolution/blob/diagnose-implicit-raw-bitwise/proposals/nnnn-implicit-raw-bitwise-conversion.md#associated-object-string-keys
private func getID(_ object: AnyObject) -> UnsafeRawPointer {
return UnsafeRawPointer(Unmanaged.passUnretained(object).toOpaque())
}

0 comments on commit fccc98a

Please sign in to comment.