<div dir="ltr">This is what withExtendedLifetime is for: <a href="https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_StandardLibrary_Functions/index.html#//apple_ref/swift/func/s:FSs20withExtendedLifetimeu0_rFzTq_Fzq_q0__q0_">https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_StandardLibrary_Functions/index.html#//apple_ref/swift/func/s:FSs20withExtendedLifetimeu0_rFzTq_Fzq_q0__q0_</a><div class="gmail_extra">
<br><div class="gmail_quote">On Tue, Jan 5, 2016 at 9:57 AM, Kristof Liliom via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I'm not a pro on Swift, but I would try to use the Unmanaged wrapper struct.<br>
<br>
Sample (might not be correct, didn't try)<br>
<br>
// Using Unmanaged<br>
func dummyfunc(backingData: dispatch_data_t) {<br>
<span class=""> var base = UnsafePointer<Void>()<br>
var size = Int()<br>
let mapped = dispatch_data_create_map(backingData, &base, &size)<br>
<br>
</span> // New code<br>
let unmanaged = Unmanaged.passUnretained(mapped)<br>
defer { unmanaged.release() }<br>
<br>
let buffer = UnsafeBufferPointer<Void>(start: UnsafePointer<Void>(base), count: size / sizeof(Void))<br>
return someFunction(buffer)<br>
}<br>
<br>
Best Regards,<br>
Chris<br>
<div class="HOEnZb"><div class="h5"><br>
> On 05 Jan 2016, at 16:57, Daniel Eggert via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
><br>
> How do I extend the lifetime of a variable, i.e. make sure that ARC is less aggressive?<br>
><br>
> clang has an attribute called objc_precise_lifetime — does Swift have something similar?<br>
><br>
><br>
> I have this code:<br>
><br>
> do {<br>
> var base = UnsafePointer<Void>()<br>
> var size = Int()<br>
> let mapped = dispatch_data_create_map(backingData, &base, &size)<br>
> let buffer = UnsafeBufferPointer<A>(start: UnsafePointer<A>(base), count: size / sizeof(A))<br>
> return someFunction(buffer)<br>
> }<br>
><br>
> 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.<br>
><br>
> 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.<br>
><br>
> How do I fix this?<br>
><br>
> /Daniel<br>
><br>
> _______________________________________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div></div>