关于initWithCoder导致的内存泄露

来自 http://longtimenoc.com/archives/关于initWithCoder导致的内存泄露

 initWithCoder 一般是通过 [NSUnarchiver unarchiveObjectWithData:NSData] 调用的, 虽然我们可能感觉代码写得没有什么问题, 但用 Performance Tool 检测还会有 leak。如果你确定自己的代码没有任何问题, 那么就可以无视 Performance Tool 的检测。

    之所以会检测到内存泄露, 一个可能的原因就是 unarchiveObjectWithData: 返回的是一个 autorelease 对象, 而在 Performance Tool 检测时, autoreleasePool 还没有释放。

    因此我们可以尝试直接在 unarchiveObjectWithData: 这个方法附近范围新建一个 autoreleasePool 然后 release 它(参考 main.m 中 autoreleasePool 的创建和 release), 如果 Performance Tool 不再提示内存泄露, 那么就证明的确没有内存泄露了。

原文地址:https://www.cnblogs.com/appwgh/p/2541470.html