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.
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() }
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.
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() }
> 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
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() }
> 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