<div dir="ltr">This is where it gets tricky.<div><br></div><div>When you create a chunk of memory using &#39;UnsafeMutablePointer.memory()&#39; or &#39;alloc()&#39;, it&#39;s as if you are programming in C with &#39;malloc&#39; and &#39;free&#39;. The memory you create and the objects you put in that memory don&#39;t participate in ARC - the runtime will not track reference counts or automatically free the memory. The memory will live on forever unless you explicitly call &#39;dealloc()&#39; later.</div><div><br></div><div>Using these APIs correctly is quite hard. If you&#39;re not careful you can leak memory (if you lose all pointers before you&#39;ve had a chance to call dealloc), or access invalid memory (you called dealloc earlier, but somewhere else you later access that memory). In a lot of cases a good thing to do is to wrap your unsafe memory buffer inside a regular Swift class, only allow the buffer to be accessed or modified through that class, and have that class be responsible for deallocating the buffer when its deinit is called. This way, you tie the lifetime of that buffer to your Swift class and ARC handles the memory management for you.</div><div><br></div><div>Best,</div><div>Austin</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 26, 2016 at 11:19 AM, Adrian Zubarev 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"><div style="word-wrap:break-word"><span class=""><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><blockquote type="cite"><span style="font-family:&#39;helvetica Neue&#39;,helvetica">It&#39;s also not clear sometimes exactly what &quot;out of bounds&quot; means - for example, you might have a big chunk of memory representing an array, and then you take a pointer to only part of that memory, representing a slice of the array. In this case you can write &quot;out of bounds&quot; of the slice, but the pointer type doesn&#39;t know that (because you are still within the range of the chunk of memory that you got from `UnsafeMutablePointer.memory()`). </span></blockquote></div> <div><br></div></span>True story. :D <div>Thank you for clarifying that to me, its a good example. Also the new pointer that I’ll get here won’t be a slice of an array just because `Memory` isn’t a slice. I’ll have to cast the pointer first, but I got the point here. ;)<div><br></div><div>One more thing:</div><div><br></div><div>- How does ARC work here when I create a new pointer to one of my allocated objects? </div><div>- Do I have 2 strong references to my main piece of memory?<br><div><br></div><div><span class=""><br> <div><div style="font-family:helvetica,arial;font-size:13px">-- <br>Adrian Zubarev<br>Sent with Airmail</div></div> <br></span><p>Am 26. Mai 2016 bei 20:07:36, Austin Zheng (<a href="mailto:austinzheng@gmail.com" target="_blank">austinzheng@gmail.com</a>) schrieb:</p></div></div></div></div><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></blockquote></div><br></div>