{"id":24941,"date":"2019-04-09T14:38:07","date_gmt":"2019-04-09T18:38:07","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=24941"},"modified":"2019-04-09T14:38:07","modified_gmt":"2019-04-09T18:38:07","slug":"implementing-nscell-copywithzone-in-swift-to-avoid-crashes-in-appkit","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/04\/09\/implementing-nscell-copywithzone-in-swift-to-avoid-crashes-in-appkit\/","title":{"rendered":"Implementing [NSCell copyWithZone:] in Swift to Avoid Crashes in AppKit"},"content":{"rendered":"<p><a href=\"https:\/\/www.corbinstreehouse.com\/blog\/2019\/04\/implementing-nscell-copywithzone-in-swift-to-avoid-crashes-in-appkit\/\">Corbin Dunn<\/a> (<a href=\"https:\/\/twitter.com\/nibroc\/status\/1115641927406313472\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/www.corbinstreehouse.com\/blog\/2019\/04\/implementing-nscell-copywithzone-in-swift-to-avoid-crashes-in-appkit\/\">\n<p>It looks like using a baseline constraint with Autolayout will cause it to copy the cell to determine the baseline.  So, if you are using AutoLayout, be aware of implicit copies that might happen behind your back!<\/p>\n<p>The trouble with <code>[NSCell copyWithZone:]<\/code> is that it uses <code>NSCopyObject<\/code>, which blindly assigns ivars from one instance to another and doesn&rsquo;t do any proper memory management. I didn&rsquo;t think this would still be an issue in Swift, but apparently it is! See my solution at the top where I simply retain the value during the copy.<\/p>\n<\/blockquote>\n<p>He writes:<\/p>\n<pre>let _ = Unmanaged&#x3C;NSColor&#x3E;.passRetained(previousTextColor)<\/pre>","protected":false},"excerpt":{"rendered":"<p>Corbin Dunn (tweet): It looks like using a baseline constraint with Autolayout will cause it to copy the cell to determine the baseline. So, if you are using AutoLayout, be aware of implicit copies that might happen behind your back! The trouble with [NSCell copyWithZone:] is that it uses NSCopyObject, which blindly assigns ivars from [&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":"2019-04-09T18:38:09Z","apple_news_api_id":"4e6d0c5b-b322-4164-a363-cf406d6ace58","apple_news_api_modified_at":"2019-04-09T18:38:10Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/ATm0MW7MiQWSjY89AbWrOWA","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":[200,69,30,1609,571,71,901],"class_list":["post-24941","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-autolayout","tag-cocoa","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\/24941","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=24941"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24941\/revisions"}],"predecessor-version":[{"id":24942,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24941\/revisions\/24942"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=24941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=24941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=24941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}