gpt4 book ai didi

objective-c - 寻找仪器之外的方法来验证和检查任何保留周期

转载 作者:行者123 更新时间:2023-11-30 13:15:48 24 4
gpt4 key购买 nike

Instruments 是一个麻烦的工具,它的内存泄漏检查经常找不到保留周期。示例:委托(delegate)不弱的委托(delegate)模式肯定会创建一个保留周期,但不知何故泄漏不会出现。保留周期肯定是泄漏吗?寻找仪器之外的日志记录机制 - 我可以将其嵌入到我的项目中来检测这一点的代码。

最佳答案

尝试了这个手动记录器,您可以使用它来初始化您正在分析的类:

struct WeakStruct {
weak var weakObject: AnyObject?
let objectName: String
}


public class RetainCycleLogger {
var weakStructs = [WeakStruct]()
public static let sharedInstance = RetainCycleLogger()

public func addObject(object: AnyObject) {
weakStructs.append(WeakStruct(weakObject: object, objectName: classNameAsString(object)))
}

func count(object: AnyObject) -> Int {
let classname = classNameAsString(object)
return weakStructs.filter({$0.objectName == classname}).flatMap({$0.weakObject}).count
}

func classNameAsString(obj: Any) -> String {
return String(reflecting: obj)
}

public func printAllCounts() {
let classes = weakStructs.filter({$0.weakObject != nil}).map({$0.objectName})
let uniqueClasses: [String] = Array(Set(classes))
for className in uniqueClasses {
print("---- Class: \(className) \(classes.filter({$0 == className}).count)")
}
}
}

关于objective-c - 寻找仪器之外的方法来验证和检查任何保留周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38235236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com