ios - Unwind segue (D->B) does not release C -
this swift 2.1 project ios 8.0+ target.
much user cybermew in this question, have set-up a->b->c->d. navigation controller , rest regular uiviewcontrollers. when unwinding d b, c not released. results in memory leak , other unintended side-effects.
as opposed question linked above, don't have nstimer know of might keep view in memory. causing happen?
code of c (trimmed):
import uikit import spritekit import avfoundation import avfoundation.avaudiosession class gameviewcontroller: uiviewcontroller { var scene: gamescene! var level: level! @iboutlet weak var gameoverpanel: uiimageview! @iboutlet weak var heropicture: uiimageview! @iboutlet weak var villainpicture: uiimageview! @iboutlet weak var spellonebutton: uibutton! @iboutlet weak var doublelife: uiswitch! let attacksound = skaction.playsoundfilenamed("attack.wav", waitforcompletion: false) var tapgesturerecognizer: uitapgesturerecognizer! var backgroundmusic: avaudioplayer = { let sess = avaudiosession.sharedinstance() if sess.otheraudioplaying { _ = try? sess.setcategory(avaudiosessioncategoryambient, withoptions: []) _ = try? sess.setactive(true, withoptions: []) } let url = nsbundle.mainbundle().urlforresource("backgroundmusic", withextension: "mp3") let player = try? avaudioplayer(contentsofurl: url!) player!.numberofloops = -1 return player! }() override func prefersstatusbarhidden() -> bool { return true } override func shouldautorotate() -> bool { return true } override func supportedinterfaceorientations() -> uiinterfaceorientationmask { return uiinterfaceorientationmask.allbutupsidedown } override func viewdidload() { super.viewdidload() let skview = view as! skview skview.multipletouchenabled = false scene = gamescene(size: skview.bounds.size) scene.scalemode = .aspectfill level = level(filename: "level_0") scene.level = level scene.addtiles() scene.swipehandler = handleswipe skview.presentscene(scene) backgroundmusic.play() begingame() } }
moved here chat:
swipehandler
declared as:
var swipehandler: ((swap) -> ())?
and swap is:
struct swap: customstringconvertible, hashable { // blabla }
and once filled swap if calls on gameviewcontroller
:
func handleswipe(swap: swap) { view.userinteractionenabled = false if level.ispossibleswap(swap) { level.performswap(swap) scene.animateswap(swap, completion: handlematches) } else { scene.animateinvalidswap(swap) { self.view.userinteractionenabled = true } }
you have reference loop causing viewcontroller not deallocated. recommend turning swipehandler
weak delegate pointer , can call handleswipe
though pointer. way, gamescene not retain strong pointers gameviewcontroller allow freed when unwind segue performed.
protocol swipehandler: class { func handleswipe(swap: swap) } class gameviewcontroller: uiviewcontroller, swipehandler { var level = level() var scene: gamescene! override func viewdidload() { super.viewdidload() scene = gamescene() scene.swipehandler = self } func handleswipe(swap: swap) { // ... } } class gamescene { weak var swipehandler: swipehandler? // function demonstrate call handleswipe func useswipehandler() { let swap = swap() swipehandler?.handleswipe(swap) } }
Comments
Post a Comment