ARC // Precise Lifetime Semantics

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

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

This is what withExtendedLifetime is for:
https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_StandardLibrary_Functions/index.html#//apple_ref/swift/func/s:FSs20withExtendedLifetimeu0_rFzTq_Fzq_q0__q0_

···

On Tue, Jan 5, 2016 at 9:57 AM, Kristof Liliom via swift-users < swift-users@swift.org> wrote:

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@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@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Thanks! That’s a very elegant solution.

/Daniel

···

On 05 Jan 2016, at 20:48, Jacob Bandes-Storch <jtbandes@gmail.com> wrote:

This is what withExtendedLifetime is for: https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_StandardLibrary_Functions/index.html#//apple_ref/swift/func/s:FSs20withExtendedLifetimeu0_rFzTq_Fzq_q0__q0_

On Tue, Jan 5, 2016 at 9:57 AM, Kristof Liliom via swift-users <swift-users@swift.org> wrote:
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@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@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users