[swift-users] ARC // Precise Lifetime Semantics

Kristof Liliom macandor.nullbit at gmail.com
Tue Jan 5 11:57:18 CST 2016


Hi,

I'm not a pro on Swift, but I would try to use the Unmanaged wrapper struct.

Sample (might not be correct, didn't try)

// Using Unmanaged
func dummyfunc(backingData: dispatch_data_t) {
    var base = UnsafePointer<Void>()
    var size = Int()
    let mapped = dispatch_data_create_map(backingData, &base, &size)

    // New code
    let unmanaged = Unmanaged.passUnretained(mapped)
    defer { unmanaged.release() }

    let buffer = UnsafeBufferPointer<Void>(start: UnsafePointer<Void>(base), count: size / sizeof(Void))
    return someFunction(buffer)
}

Best Regards,
Chris

> On 05 Jan 2016, at 16:57, Daniel Eggert via swift-users <swift-users at swift.org> wrote:
> 
> How do I extend the lifetime of a variable, i.e. make sure that ARC is less aggressive?
> 
> clang has an attribute called objc_precise_lifetime — does Swift have something similar?
> 
> 
> I have this code:
> 
>  do {
>    var base = UnsafePointer<Void>()
>    var size = Int()
>    let mapped = dispatch_data_create_map(backingData, &base, &size)
>    let buffer = UnsafeBufferPointer<A>(start: UnsafePointer<A>(base), count: size / sizeof(A))
>    return someFunction(buffer)
>  }
> 
> I need the ‘mapped’ variable to stay in scope (i.e. not be released) until the end of the scope, but ARC is free to release it immediately — in fact the compiler warns me that the immutable value is never used.
> 
> But the API contract with dispatch_data_create_map(3) is that the values in ‘base’ and ‘size’ are only valid as long as ‘mapped’ is around. The above code is passing a buffer into ‘someFunction’ that’s no longer valid.
> 
> How do I fix this?
> 
> /Daniel
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-users mailing list