LeakDetector 1.0.0

LeakDetector 1.0.0

Quang DD9 维护。



 
依赖
RxSwift~> 5.0
RxCocoa~> 5.0
 

LeakDetector

Platforms License

Swift Package Manager Carthage compatible CocoaPods compatible

iOS 运行时内存泄漏检测

安装

依赖管理器

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它

$ gem install cocoapods

要使用 CocoaPods 将 LeakDetector 集成到您的 Xcode 项目中,请在其 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'LeakDetector', '~> 1.0.0'

然后,运行以下命令

$ pod install
Carthage

Carthage 是一个去中心化的依赖管理器,它会自动将框架添加到您的 Cocoa 应用程序中。

您可以使用以下命令使用 Homebrew 安装 Carthage

$ brew update
$ brew install carthage

要使用 Carthage 将 LeakDetector 集成到您的 Xcode 项目中,请在其 Cartfile 中指定它

github "duyquang91/LeakDetector" ~> 1.0.0
Swift 包管理器

要将 LeakDetector 作为 Swift 包管理器 包使用,只需在您的 Package.swift 文件中添加以下内容。

// swift-tools-version:4.2

import PackageDescription

let package = Package(
    name: "HelloLeakDetector",
    dependencies: [
        .package(url: "https://github.com/duyquang91/LeakDetector.git", .upToNextMajor(from: "1.0.0"))
    ],
    targets: [
        .target(name: "HelloLeakDetector", dependencies: ["LeakDetector"])
    ]
)

手动

如果您不想使用上述任何依赖项管理器,您可以手动将 LeakDetector 集成到项目中。

Git 子模块

  • 打开终端,cd 到你的顶级项目目录,并运行以下命令 "如果" 你的项目尚未作为一个 Git 仓库初始化
$ git init
  • 通过运行以下命令将 LeakDetector 添加为 Git submodule
$ git submodule add https://github.com/duyquang91/LeakDetector.git
$ git submodule update --init --recursive
  • 打开新的 LeakDetector 文件夹,并将 LeakDetector.xcodeproj 拖入你的应用程序 Xcode 项目的项目导航器中。

    它应该位于你的应用程序蓝色项目图标之下。它在所有其他 Xcode 组之上或之下并不重要。

  • 在项目导航器中选中 LeakDetector.xcodeproj 并验证其部署目标与你的应用程序目标匹配。

  • 然后,在项目导航器中(蓝色项目图标)选择你的应用程序项目,进入目标配置窗口,并在侧边栏的 "Targets" 标题下选择应用程序目标。

  • 在该窗口顶部的选项卡栏中,打开 "General" 面板。

  • 在 "Embedded Binaries" 部分的 + 按钮上点击。

  • 你会看到两个不同的 LeakDetector.xcodeproj 文件夹,每个文件夹中包含两个不同版本的 LeakDetector.framework,它们都嵌套在 Products 文件夹中。

    你选择哪个 Products 文件夹并不重要。

  • 选择 LeakDetector.framework

  • 就是这样了!

LeakDetector.framework 会自动添加为目标依赖项,并在复制文件构建阶段链接为框架和嵌入框架,这正是你用于模拟器和设备的所有需要。

嵌入的二进制文件

  • https://github.com/duyquang91/LeakDetector/releases 下载最新版本。
  • 然后,在项目导航器中(蓝色项目图标)选择你的应用程序项目,进入目标配置窗口,并在侧边栏的 "Targets" 标题下选择应用程序目标。
  • 在该窗口顶部的选项卡栏中,打开 "General" 面板。
  • 在 "Embedded Binaries" 部分的 + 按钮上点击。
  • 添加下载的 LeakDetector.framework
  • 就是这样了!

使用方法

让我们假设我们有一个可以泄漏的类

class LeakableObject {
  var otherObject: LeakableObject!

  init(otherObject: LeakableObject? = nil) {
    self.otherObject = otherObject
  }
}

下面两个 LeakableObject 实例将会泄漏

let object1 = LeakableObject()
let object2 = LeakableObject(otherObject: object1)
object1.otherObject = object2

使用 LeakDetector 可以在运行时检测到泄漏

import LeakDetector

// When we expecting the deallocation of 2 instances of LeakableObject, such as view controller keep these instances is dismissed
LeakDetector.instance.expectDeallocate(object: object1)
LeakDetector.instance.expectDeallocate(object: object2)

一个断言失败将会抛出,并且应用程序会崩溃以让开发者意识到并修复泄漏。默认情况下,LeakDetector 是禁用的,我们应该只在调试模式下启用。

func application(_ application: UIApplication, didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  #if Debug
      LeakDetector.isEnabled = true
  #endif
  return true
}

你可以打开 Demo 项目来探索使用 LeakDetector 的更多用例。

贡献

欢迎提交问题和拉取请求!

作者

@SteveDao

许可证

LeakDetector 在MIT许可下发布。有关详细信息,请参阅LICENSE