LeakSanitizer on macOS

Hi everyone,

I’ve been working through the Swift.org guide on Memory Leaks and Usage using a macOS CLI tool. The tool makes use of the sample MemoryLeaker example as briefed in the above post.

public class MemoryLeaker {
   var closure: () -> Void = { () }
   
   public init() {}
   
   public func doNothing() {}
   
   public func doSomethingThatLeaks() {
      self.closure = {
         // This will leak as it'll create a permanent reference cycle:
         //
         //     self -> self.closure -> self
         self.doNothing()
      }
   }
}
@inline(never) // just to be sure to get this in a stack trace
func myFunctionDoingTheAllocation() {
   let thing = MemoryLeaker()
   thing.doSomethingThatLeaks()
}

myFunctionDoingTheAllocation()

However, when running with the Address Sanitizer enabled via the Xcode toolchain(also with ASAN_OPTIONS=detect_leaks=1), I'm not seeing any leak reports on exit:

swift run --sanitize=address
# with export ASAN_OPTIONS=detect_leaks=1

While I've confirmed the leak is detectable using the native leaks CLI and Instruments, I'm curious about the specific behavior of the Swift/Xcode toolchain regarding LSan.

Can someone help point out what I'm missing to enable the LeakSanitizer component?

1 Like