Array becomes inaccessible during burn API notification chain
<p>I’m writing an app that burns to optical disks and there’s a strange problem when returning from the burn progress panel.</p>

<p>I call the disk recording setup sheet like this:</p>

<pre><code>let setup_panel = DRBurnSetupPanel()
let passed = UnsafeMutableRawPointer(Unmanaged.passUnretained(self.disk_buckets[current_burn_disk]).toOpaque())
setup_panel.beginSetupSheet(for: self.window, modalDelegate: self, didEnd: #selector(self.burnSetupPanelDidEnd(_:return_code:context_info:)), contextInfo: passed)
</code></pre>

<p><code>disk_buckets</code> is an array of classes each containing a reference to a <code>DRTrack</code> to be burnt - I pass an element so that I can deal with the <code>DRTrack</code> reference later.</p>

<p>After the setup panel is dismissed, the following method is then called:</p>

<pre><code>func burnSetupPanelDidEnd(_ panel: DRBurnSetupPanel, return_code: Int, context_info: UnsafeMutableRawPointer) {
//…. some code ….
var track:DRTrack = self.disk_buckets[self.current_burn_disk].vDRTrack!
NotificationCenter.default.addObserver(self, selector: #selector(self.progressDidEnd), name: NSNotification.Name(rawValue: BTVDiscBurnDidEndNotification), object: nil)
self.objc_panel.presentDiscProgressPanel(self.window, burner: panel.burnObject(), layout: track!)
}
</code></pre>

<p><code>objc_panel</code> is a obj-c singleton that initialises and presents a disc burning progress panel (I did it this way as doing it in Swift is buggy - the progress panel is displayed as a tiny window instead of the default OS one - bug report has been sent to Apple). At the end of the burn <code>objc_panel</code> posts the <code>BTVDiscBurnDidEndNotification</code> notification to let the <code>AppDelegate</code> know it’s finished and so to call:</p>

<pre><code>func progressDidEnd(_ note: Notification?) {
        print (“DEBUG count: \(self.disk_buckets.count)")
        print (“DEBUG first element: \(self.disk_buckets[0])")
//…. more code ….
}
</code></pre>

<p>All goes well until the end of the chain when any attempt to access the first element (or any, for that matter) causes a crash:</p>

<pre><code>2016-10-04 14:01:34.654 DiskSpan[4025:220213] *** -[DiskSpan.BackupDisk retain]: message sent to deallocated instance 0x6000cafc0f40
</code></pre>

<p>(BackupDisk being the class that populates the array)</p>

<p>However, the first line prints out a correct count of the array i.e. 1. So how come I can't access anything in the array during a notification post?</p>

<p>EDIT: I tried <code>append</code>ing a dummy item to the array before reading it back and still the crash only occurs when trying to read the array - not write to it.</p>

<p>It should be noted that this worked under Swift 2 and has only become a problem since Swift 3!</p>