[swift-users] Why does this leak?

Rick Aurbach rlaurb at icloud.com
Sun Mar 26 12:43:48 CDT 2017


I have a situation where I have a leak that I do not understand. I would be very grateful if someone could explain it to me and offer an idea of how I can make the pattern work without leaking:

Consider two code snippets, the first of which leaks, while the second does not.

Case 1: This example leaks 2 32-byte objects..

in FontSelectorDialog.swift:
	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
	switch segue.identifier! {
	< ... >
	
	case "ChooseTextColor" :
		let target = segue.destination as! ColorChooser
		target.textChooserType = .text	// <===
		< ... >
		}
		
	case "ChooseBackgroundColor" :
		let target = segue.destination as! ColorChooser
		target.textChooserType = .bkgnd	// <===
		< ... >
		}
		
	default : break
	}

in ColorChooser.swift:

internal enum ColorCat {			// <===
	case: .text				// <===
	case: .bkgnd				// <===
}						// <===

internal class ColorChooser : UITableViewController {
	internal var textChooserType : ColorCat = .text	// <===
	< ... >
}

Case 2: This example does not leak...

in FontSelectorDialog.swift:
	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
	switch segue.identifier! {
	< ...>
		
	case "ChooseTextColor" :
		let target = segue.destination as! ColorChooser
		target.textChooserType = true	// <===
		< ... >
		}
		
	case "ChooseBackgroundColor" :
		let target = segue.destination as! ColorChooser
		target.textChooserType = false	// <===
		< ... >
		}
		
	default : break
	}

in ColorChooser.swift:

internal class ColorChooser : UITableViewController {
	internal var textChooserType : Bool = true	// <===
	< ... >
}

Can someone explain why? And how can I implement this using an enum. I want to use an enum here because:
(a) it is easier to read and understand and seems more “swiftly”
(b) Someday, I may want to add additional options, which would be easy with an enum but difficult using the current (Bool) implementation.

Cheers,

Rick Aurbach

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170326/f0e2d3e9/attachment.html>


More information about the swift-users mailing list