泄漏检测


一款检测 Swift 潜在泄漏的工具

安装

brew install mint
mint install yume190/LeakDetect

用法

leakDetect \
    --module "SCHEME NAME" \
    --file Sample.xcworkspace

leakDetect \
    --module "SCHEME NAME" \
    --file Sample.xcodeproj

# spm
leakDetect \
    --module TARGET_NAME \
    --file .

# file
leakDetect \
    --sdk macosx \
    --file xxx.swift

用法(Github Action)

jobs:
  build:

    runs-on: macos-latest

    steps:
    - uses: actions/checkout@v3
    - uses: yume190/LeakDetect@0.0.8
      with:
        # https://github.com/antranapp/LeakDetector
        module: LeakDetectorDemo
        file: LeakDetectorDemo.xcworkspace
        token: ${{secrets.GITHUB_TOKEN}}

跳过列表

默认路径是 .leakdetect.yml,或者你可以使用 --skip list.yml

functions:
  # objc function `Dispatch.DispatchQueue.main.async {...}`
  - module: Dispatch
    types:
    - name: DispatchQueue
      instance_functions:
      - async
      - asyncAfter
  # static function `UIKit.UIView.UIView.anmiate {...}`
  - module: UIKit.UIView
    types:
    - name: UIView
      static_functions:
      - animate
  # Some Special case
  - module: YOUR_MODULE_NAME
    types:
    # global function `func escape(...) {}`
    - name: ""
      instance_functions:
      - escape
    # constructor `struct A {...}`
    # A(...) {}
    - name: A
      static_functions:
      - init
    # Nested Type A.B
    - name: A.B
    # Generic Type C<T>.D<U>
    # ignore generic
    - name: C.D

模式

赋值

检测赋值实例函数。

  1. x = self.func

    • 检查函数是否为实例函数
    • 检查 self 是否为 struct
  2. y(self.func)

    • 检查函数是否为实例函数
    • 检查参数是否为 逃逸闭包

参见 不要使用这种语法!

func escape(block: @escaping () -> Void) {}
class Temp {
  func instanceFunction() {}
  func leak() {
    let x = self.instanceFunction
    escape(block: self.instanceFunction)
  }
}

捕获

检测实例是否被代码块(闭包/函数)捕获。

示例

# Example:
git clone https://github.com/antranapp/LeakDetector
cd LeakDetector

leakDetect \
    --module LeakDetectorDemo \
    --file LeakDetectorDemo.xcworkspace