<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">So letting the bytes escape from the closure could potentially be in that territory of “bad things may happen”. If you know for certain that the Data will not be mutated and it’s underlying storage will last for the duration of the usage then you should be able to get away with it.</div><div class=""><br class=""></div><div class="">The bytes pointer is an inner pointer return from the encapsulating object backing the struct Data. Since that backing object can potentially be deallocated via mutation or the buffer may be reallocated (which very often can cause the base address of the pointer to change) we decided it would be safer to use a closure to ensure the lifespan of the bytes over the access to the bytes.</div><div class=""><br class=""></div><div class="">Perhaps it might be best to understand more of what you are trying to do to offer a better solution.</div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 9, 2016, at 8:39 AM, Ray Fix via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">I am writing a tutorial and creating a wrapper on streaming compression. &nbsp;In the code, I setup the stream source pointer: (input is of type Data)</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures" class="">// set up the stream with the input source</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; input.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">withUnsafeBytes</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { (bytes: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UnsafePointer</span><span style="font-variant-ligatures: no-common-ligatures" class="">&lt;</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">&gt;) </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; stream.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">src_ptr</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = bytes</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; }</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="font-family: Helvetica; font-size: 12px;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; stream.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">src_size</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = input.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class=""><br class=""></span></div></div><div style="font-family: Helvetica; font-size: 12px;" class="">Input is guaranteed to be alive for the entire lifetime of the stream. &nbsp;Everything seems to work fine but I thought I remember reading somewhere that it is bad form to let the bytes escape out of the withUnsafeBytes closure. &nbsp;Anyone know if I'm I dipping into undefined behavior here?</div><div style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px;" class="">Thanks in advanced,</div><div style="font-family: Helvetica; font-size: 12px;" class="">Ray</div><div class=""><br class=""></div></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>