<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">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</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&#39;m not a pro on Swift, but I would try to use the Unmanaged wrapper struct.<br>
<br>
Sample (might not be correct, didn&#39;t try)<br>
<br>
// Using Unmanaged<br>
func dummyfunc(backingData: dispatch_data_t) {<br>
<span class="">    var base = UnsafePointer&lt;Void&gt;()<br>
    var size = Int()<br>
    let mapped = dispatch_data_create_map(backingData, &amp;base, &amp;size)<br>
<br>
</span>    // New code<br>
    let unmanaged = Unmanaged.passUnretained(mapped)<br>
    defer { unmanaged.release() }<br>
<br>
    let buffer = UnsafeBufferPointer&lt;Void&gt;(start: UnsafePointer&lt;Void&gt;(base), count: size / sizeof(Void))<br>
    return someFunction(buffer)<br>
}<br>
<br>
Best Regards,<br>
Chris<br>
<div class="HOEnZb"><div class="h5"><br>
&gt; On 05 Jan 2016, at 16:57, Daniel Eggert via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; How do I extend the lifetime of a variable, i.e. make sure that ARC is less aggressive?<br>
&gt;<br>
&gt; clang has an attribute called objc_precise_lifetime — does Swift have something similar?<br>
&gt;<br>
&gt;<br>
&gt; I have this code:<br>
&gt;<br>
&gt;  do {<br>
&gt;    var base = UnsafePointer&lt;Void&gt;()<br>
&gt;    var size = Int()<br>
&gt;    let mapped = dispatch_data_create_map(backingData, &amp;base, &amp;size)<br>
&gt;    let buffer = UnsafeBufferPointer&lt;A&gt;(start: UnsafePointer&lt;A&gt;(base), count: size / sizeof(A))<br>
&gt;    return someFunction(buffer)<br>
&gt;  }<br>
&gt;<br>
&gt; 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>
&gt;<br>
&gt; 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>
&gt;<br>
&gt; How do I fix this?<br>
&gt;<br>
&gt; /Daniel<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; swift-users mailing list<br>
&gt; <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
&gt; <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>