Swift:初始化抛出时如何释放属性?



此示例代码存在内存泄漏。

指针 1和指针2在 Person 成功初始化之前分配。如果init函数引发错误。deinit函数永远不会被执行。因此,指针 1和指针2永远不会被释放。

import XCTest
class Person {
// case1
let pointer1: UnsafeMutablePointer<Int> = UnsafeMutablePointer<Int>.allocate(capacity: 1)
// case2
let pointer2: UnsafeMutablePointer<Int>
let name: String
init(name: String) throws {
// case2
self.pointer2 = UnsafeMutablePointer<Int>.allocate(capacity: 1)

if name == "UnsupportName" {
throw NSError()
}
self.name = name
}
deinit {
pointer1.deallocate()
pointer2.deallocate()
}
}
class InterestTests: XCTestCase {
func testExample() {
while true {
_ = try? Person(name: "UnsupportName")
}
}
}

有时逻辑非常复杂。在我的真实案例中。有很多allocatethrowsifguard.有些很难控制它。

有什么方法可以避免这种内存泄漏吗?

这是一个类似的问题:https://forums.swift.org/t/deinit-and-failable-initializers/1199

在您的特定示例中,解决方案很简单。在解决所有可能的故障之前,不要分配任何内存:

class Person {
let aPointer: UnsafeMutablePointer<Int> // Do not allocate here.
let name: String
init(name: String) throws {
// Validate everything here
guard name != "UnsupportName" else {
throw NSError()
}
// After this point, no more throwing:
self.name = name
// Move the allocation here
self.aPointer = UnsafeMutablePointer.allocate(capacity: 1)
}
deinit {
aPointer.deallocate()
}
}

但更通用的解决方案是像在需要管理错误的任何其他位置一样使用 do/catch:

class Person {
let aPointer = UnsafeMutablePointer<Int>.allocate(capacity: 1)
let name: String
init(name: String) throws {
do {
if name == "UnsupportName" {
throw NSError()
}
self.name = name
} catch let e {
self.aPointer.deallocate()
throw e
}
}
deinit {
aPointer.deallocate()
}
}

我很想把.allocate移到init里面,只是为了让它更清楚地看到正在发生的事情。关键点是,你应该先分配所有内存,在任何东西可以抛出之前(所以你知道你可以全部释放(,或者在最后一次抛出之后分配所有内存(所以你知道你没有任何要释放的东西(。


查看您添加的解决方案,没关系,但建议围绕它的危险逻辑。最好解开它,将分配放入它们自己的对象中(这几乎肯定会摆脱 UnsafeMutablePointers;在一个类中需要很多指针是非常可疑的(。

也就是说,IMO有更干净的方法来沿着这条路径构建错误处理。

extension UnsafeMutablePointer {
static func allocate(capacity: Int, withCleanup cleanup: inout [() -> Void]) -> UnsafeMutablePointer<Pointee> {
let result = allocate(capacity: capacity)
result.addTo(cleanup: &cleanup)
return result
}
func addTo(cleanup: inout [() -> Void]) {
cleanup.append { self.deallocate() }
}
}

这使得 UnsafeMutablePointer 将清理信息附加到数组中,而不是创建大量defer块,这会增加在清理过程中丢失一个块的风险。

有了这个,你的初始化看起来像:

init(name: String) throws {
var errorCleanup: [() -> Void] = []
defer { for cleanup in errorCleanup { cleanup() } }
// deallocate helper for case1
pointer1.addTo(cleanup: &errorCleanup)
// case2
self.pointer2 = UnsafeMutablePointer<Int>.allocate(capacity: 1, withCleanup: &errorCleanup)
// case ...

if name == "UnsupportName" {
throw NSError()
}
self.name = name
// In the end. set deallocate helpers to nil
errorCleanup.removeAll()
}

当然,这设置了打电话给allocate(capacity:)而不是allocate(capacity:withCleanup:)的危险。因此,您可以通过将其包装到另一种类型中来解决此问题;自动解除分配自身的引用类型。

class SharedPointer<Pointee> {
let ptr: UnsafeMutablePointer<Pointee>
static func allocate(capacity: Int) -> SharedPointer {
return .init(pointer: UnsafeMutablePointer.allocate(capacity: capacity))
}
init(pointer: UnsafeMutablePointer<Pointee>) {
self.ptr = pointer
}
deinit {
ptr.deallocate()
}
}

这样,这就变成了(不需要设计(:

class Person {
// case1
let pointer1 = SharedPointer<Int>.allocate(capacity: 1)
// case2
let pointer2: SharedPointer<Int>
let name: String
init(name: String) throws {
// case2
self.pointer2 = SharedPointer<Int>.allocate(capacity: 1)
if name == "UnsupportName" {
throw NSError()
}
self.name = name
}
}

您可能希望编写各种帮助程序来处理.ptr

当然,这可能会导致您构建特定版本的 SharedPointer 来处理各种事情(例如"father"而不是"int"(。如果你继续沿着这条路走下去,你会发现 UnsafeMutablePointer 消失了,问题就消失了。但是你不必走那么远,SharedPointer 会为你完成这项工作。

我找到了解决问题的方法。

import XCTest
class Person {
// case1
let pointer1: UnsafeMutablePointer<Int> = UnsafeMutablePointer<Int>.allocate(capacity: 1)
// case2
let pointer2: UnsafeMutablePointer<Int>
let name: String
init(name: String) throws {
// deallocate helper for case1
var deallocateHelper1: UnsafeMutablePointer<Int>? = self.pointer1
defer {
deallocateHelper1?.deallocate()
}
// case2
self.pointer2 = UnsafeMutablePointer<Int>.allocate(capacity: 1)
var deallocateHelper2: UnsafeMutablePointer<Int>? = self.pointer2
defer {
deallocateHelper2?.deallocate()
}
// case ... 

if name == "UnsupportName" {
throw NSError()
}
self.name = name
// In the end. set deallocate helpers to nil
deallocateHelper1 = nil
deallocateHelper2 = nil
}
deinit {
pointer1.deallocate()
pointer2.deallocate()
}
}
class InterestTests: XCTestCase {
func testExample() {
while true {
_ = try? Person(name: "UnsupportName")
}
}
}

另一种解决方案。

class Person {
let name: String
let pointer1: UnsafeMutablePointer<Int>
let pointer2: UnsafeMutablePointer<Int>
init(name: String) throws {
var pointers: [UnsafeMutablePointer<Int>] = []
do {
let pointer1 = UnsafeMutablePointer<Int>.allocate(capacity: 1)
pointers.append(pointer1)
let pointer2 = UnsafeMutablePointer<Int>.allocate(capacity: 1)
pointers.append(pointer2)
if name == "Unsupported Name" {
throw NSError()
}
self.pointer1 = pointer1
self.pointer2 = pointer2
self.name = name
} catch {
pointers.forEach { $0.deallocate() }
throw error
}
}
deinit {
pointer1.deallocate()
pointer2.deallocate()
}
}

相关内容

  • 没有找到相关文章

最新更新