{"id":23070,"date":"2018-10-12T15:47:52","date_gmt":"2018-10-12T19:47:52","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=23070"},"modified":"2018-10-15T21:31:33","modified_gmt":"2018-10-16T01:31:33","slug":"working-around-a-swift-nonmutating-crash","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/10\/12\/working-around-a-swift-nonmutating-crash\/","title":{"rendered":"Working Around a Swift &ldquo;nonmutating&rdquo; Crash"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/nataliya_bg\/status\/1046400504522305537\">Nataliya Patsovska<\/a> (via <a href=\"https:\/\/twitter.com\/fpillet\/status\/1046518115910209536\">Florent Pillet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/nataliya_bg\/status\/1046400504522305537\">\n<p>We recently found a mysterious bug in our framework Flow present only when building with Xcode 10:<\/p>\n<p>-Mutating an object on the same line it was created on would cause BAD_ACCESS crash.<\/p>\n<p>-Splitting the line into 2 lines would work fine.<\/p>\n<p>Of course that was a workaround, not the fix.<\/p>\n<p>[&#8230;]<\/p>\n<p>Turns out we were using the &ldquo;nonmutating&rdquo; keyword in a protocol extension and the compiler decided it is safe to deallocate the object implementing the protocol too early.<\/p>\n<p>Moving the setter to the class somehow worked[&#8230;]<\/p><\/blockquote>\n\n<p>See also: <a href=\"http:\/\/khanlou.com\/2017\/11\/mutating-and-nonmutating-functions\/\">Mutating And Nonmutating Functions<\/a>.<\/p>\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2016\/01\/08\/exploring-swift-arrays-implementation\/\">Exploring Swift Array&rsquo;s Implementation<\/a>.<\/p>\n\n<p>Update (2018-10-15): Joe Groff notes the <a href=\"https:\/\/bugs.swift.org\/browse\/SR-8990\">bug page<\/a> and a <a href=\"https:\/\/github.com\/apple\/swift\/pull\/19902\">potential fix<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Nataliya Patsovska (via Florent Pillet): We recently found a mysterious bug in our framework Flow present only when building with Xcode 10: -Mutating an object on the same line it was created on would cause BAD_ACCESS crash. -Splitting the line into 2 lines would work fine. Of course that was a workaround, not the fix. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2018-10-12T19:47:55Z","apple_news_api_id":"4e8bfe9d-05aa-4f05-944f-45b4db3f11d8","apple_news_api_modified_at":"2018-10-16T01:31:37Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/ATov-nQWqTwWUT0W02z8R2A","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[131,31,1610,30,1609,571,71,901],"class_list":["post-23070","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bug","tag-ios","tag-ios-12","tag-mac","tag-macos-10-14","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=23070"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23070\/revisions"}],"predecessor-version":[{"id":23101,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23070\/revisions\/23101"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=23070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=23070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=23070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}