{"id":5981,"date":"2019-04-09T07:54:26","date_gmt":"2019-04-09T14:54:26","guid":{"rendered":"https:\/\/www.corbinstreehouse.com\/blog\/?p=5981"},"modified":"2019-04-09T08:43:11","modified_gmt":"2019-04-09T15:43:11","slug":"implementing-nscell-copywithzone-in-swift-to-avoid-crashes-in-appkit","status":"publish","type":"post","link":"https:\/\/www.corbinstreehouse.com\/blog\/2019\/04\/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 id=\"top\" \/>\n<p><strong>Quick Summary<\/strong><\/p>\n<p>If you add a Swift property to an NSTextFieldCell subclass and you may suddenly start getting random crashes! Implement an override of [NSCell copyWithZone:] that retains any properties to fix this:<\/p>\n<style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist95621250\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-tableviewtextfieldcell-swift\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-swift  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"TableViewTextFieldCell.swift content, created by corbinstreehouse on 02:43PM on April 09, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"TableViewTextFieldCell.swift\">\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC1\" class=\"blob-code blob-code-inner js-file-line\">\/\/ Swift 3.0<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC2\" class=\"blob-code blob-code-inner js-file-line\">class TableViewTextFieldCell: NSTextFieldCell {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC3\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC4\" class=\"blob-code blob-code-inner js-file-line\">    private var previousTextColor: NSColor?<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC5\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC6\" class=\"blob-code blob-code-inner js-file-line\">    override func copy(with zone: NSZone? = nil) -&gt; Any {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC7\" class=\"blob-code blob-code-inner js-file-line\">        let result: TableViewTextFieldCell = super .copy(with: zone) as! TableViewTextFieldCell<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC8\" class=\"blob-code blob-code-inner js-file-line\">        if let previousTextColor = result.previousTextColor {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC9\" class=\"blob-code blob-code-inner js-file-line\">            \/\/ Add the needed retain now<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC10\" class=\"blob-code blob-code-inner js-file-line\">            let _ = Unmanaged&lt;NSColor&gt;.passRetained(previousTextColor)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC11\" class=\"blob-code blob-code-inner js-file-line\">        }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC12\" class=\"blob-code blob-code-inner js-file-line\">        return result<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC13\" class=\"blob-code blob-code-inner js-file-line\">    }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-tableviewtextfieldcell-swift-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-tableviewtextfieldcell-swift-LC14\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/corbinstreehouse\/d5601d0f543999c66c1a8111ae5a5ec1\/raw\/7e83a1802c245c936097f891f2f91df93825c0f0\/TableViewTextFieldCell.swift\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/corbinstreehouse\/d5601d0f543999c66c1a8111ae5a5ec1#file-tableviewtextfieldcell-swift\" class=\"Link--inTextBlock\">\n          TableViewTextFieldCell.swift\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n\n<p><strong>Gory Details<\/strong><\/p>\n<p>I hit this in a little app that I\u2019m working on, and I was a bit stumped as to what was happening. Using Zombies in Instruments revealed an overrelease of a color in a simple NSTextFieldCell subclass that added an NSColor property.<\/p>\n<style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist95622269\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-customtextfieldcell-swift\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-swift  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"CustomTextFieldCell.swift content, created by corbinstreehouse on 03:39PM on April 09, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"CustomTextFieldCell.swift\">\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC1\" class=\"blob-code blob-code-inner js-file-line\">\/\/ Custom text colors don&#39;t automagically invert.<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC2\" class=\"blob-code blob-code-inner js-file-line\">class TableViewTextFieldCell: NSTextFieldCell {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC3\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC4\" class=\"blob-code blob-code-inner js-file-line\">    private var previousTextColor: NSColor?<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC5\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC6\" class=\"blob-code blob-code-inner js-file-line\">    override var backgroundStyle: NSView.BackgroundStyle {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC7\" class=\"blob-code blob-code-inner js-file-line\">        get {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC8\" class=\"blob-code blob-code-inner js-file-line\">            return super.backgroundStyle<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC9\" class=\"blob-code blob-code-inner js-file-line\">        }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC10\" class=\"blob-code blob-code-inner js-file-line\">        set(newBackgroundStyle) {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC11\" class=\"blob-code blob-code-inner js-file-line\">            \/\/ If we are going to light because we are selected, save off the old color so we can restore it<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC12\" class=\"blob-code blob-code-inner js-file-line\">            if self.backgroundStyle == .light &amp;&amp; newBackgroundStyle == .dark {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC13\" class=\"blob-code blob-code-inner js-file-line\">                previousTextColor = self.textColor<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC14\" class=\"blob-code blob-code-inner js-file-line\">                self.textColor = NSColor.white \/\/ or a named color?<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC15\" class=\"blob-code blob-code-inner js-file-line\">            } else if self.backgroundStyle == .dark &amp;&amp; newBackgroundStyle == .light {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC16\" class=\"blob-code blob-code-inner js-file-line\">                if previousTextColor != nil {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC17\" class=\"blob-code blob-code-inner js-file-line\">                    self.textColor = previousTextColor<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC18\" class=\"blob-code blob-code-inner js-file-line\">                    previousTextColor = nil<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC19\" class=\"blob-code blob-code-inner js-file-line\">                }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC20\" class=\"blob-code blob-code-inner js-file-line\">            }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC21\" class=\"blob-code blob-code-inner js-file-line\">            super.backgroundStyle = newBackgroundStyle<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC22\" class=\"blob-code blob-code-inner js-file-line\">            <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L23\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"23\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC23\" class=\"blob-code blob-code-inner js-file-line\">        }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L24\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"24\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC24\" class=\"blob-code blob-code-inner js-file-line\">    }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-customtextfieldcell-swift-L25\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"25\"><\/td>\n          <td id=\"file-customtextfieldcell-swift-LC25\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/corbinstreehouse\/2ac633e04204734948a4395e73c716e0\/raw\/5caf81dceb54678273cad7cedf5a54ed27059028\/CustomTextFieldCell.swift\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/corbinstreehouse\/2ac633e04204734948a4395e73c716e0#file-customtextfieldcell-swift\" class=\"Link--inTextBlock\">\n          CustomTextFieldCell.swift\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n\n<p>I\u2019m using a View-Based NSTableView, and I know that a cell-based NSTableView would have some issues unless you implement [NSCell copyWithZone:], but that shouldn\u2019t be needed for my particular case.\u00a0<\/p>\n<p>The Zombies seemed to reveal that the deallocated cell was doing something bad. I did a little bit of digging by the logging of addresses, and discovered my NSColor property was duplicated in a few locations. That was strange, as the instances should have been unique. I found out that it was being copied by AppKit! Here\u2019s the backtrace I hit upon:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"display: block; margin-left: auto; margin-right: auto;\" title=\"copywithZone_cell.png\" src=\"https:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2019\/04\/copywithZone_cell.png\" alt=\"copy point\" width=\"404\" height=\"171\" border=\"0\" \/><\/p>\n<p>It looks like using a baseline constraint with Autolayout will cause it to copy the cell to determine the baseline. \u00a0So, if you are using AutoLayout, be aware of implicit copies that might happen behind your back!<\/p>\n<p>The trouble with [NSCell copyWithZone:] is that it uses NSCopyObject, which blindly assigns ivars from one instance to another and doesn\u2019t do any proper memory management. I didn\u2019t 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<p>I did some searching and numerous people are hitting this problem:<\/p>\n<p>Developer Forums:\u00a0<a href=\"https:\/\/forums.developer.apple.com\/thread\/66154\">Swift NSCopying on the Developer<\/a><\/p>\n<p>StackOverflow: <a href=\"https:\/\/stackoverflow.com\/questions\/33037943\/copying-nstextfieldcell-subclass-in-swift-causes-crash\">Copying NSTextFieldCell subclass in Swift causes crash<\/a><\/p>\n<p>Swift.org: <a href=\"https:\/\/bugs.swift.org\/browse\/SR-4756\">If NSCopyObject is called on a Swift class with stored properties, it can cause a crash<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick Summary If you add a Swift property to an NSTextFieldCell subclass and you may suddenly start getting random crashes! Implement an override of [NSCell copyWithZone:] that retains any properties to fix this: Gory Details&#8230; <a class=\"read-more\" href=\"https:\/\/www.corbinstreehouse.com\/blog\/2019\/04\/implementing-nscell-copywithzone-in-swift-to-avoid-crashes-in-appkit\/\">[read more]<\/a><\/p>\n","protected":false},"author":1,"featured_media":5986,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,6,86],"tags":[199,63,201,203,202,204,205,72,200],"class_list":["post-5981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apple","category-cocoa","category-coding","tag-bugs","tag-cocoa","tag-nscell","tag-nsoutlineview","tag-nstableview","tag-nstextfield","tag-nstextfieldcell","tag-programming","tag-swift"],"jetpack_featured_media_url":"https:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2019\/04\/copywithZone_cell.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/5981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/comments?post=5981"}],"version-history":[{"count":5,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/5981\/revisions"}],"predecessor-version":[{"id":5988,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/5981\/revisions\/5988"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media\/5986"}],"wp:attachment":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media?parent=5981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/categories?post=5981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/tags?post=5981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}