0

I have a question about the attached error message. It is showing an EXC_BAD_ACCESS code around the variable closeTree1, defined below:

let closeTree1 = SKSpriteNode(texture: SKTexture(imageNamed: "tree1"))

As you can see at the bottom of the right side of the debug area, I have a print line that shows that Xcode has not deallocated closeTree1:

closeTree1: name:'(null)' texture:[ 'tree1' (721 x 1112)] position:{2902.64990234375, 577.58203125} scale:{1.00, 1.00} size:{420, 655.16400146484375} anchor:{0.5, 0.5} rotation:0.00

I also haven't had a problem with early deallocation in Swift before, so I think it's related to something else. The research I've done says that EXC_BAD_ACCESS comes from a variable being overreleased and is more of an Objective-C problem.

This crash occurs inconsistently and always when I am resetting the scene. I'm not transitioning scenes, but removing all the children from the same scene and adding everything back.

I've used NSZombies and breakpoint but to no avail. I've also been working on this issue for several days and seen several other EXC_BAD_ACCESS errors on seemingly trivial lines. If anyone can shed some light on this that would be greatly appreciated.

Error Message

jelmore
  • 1
  • 3
  • EXEC_BAD_ACCESS come from trying to reference anything that is nil, for which you expect to be non-nil. swift guards against it better than objc does, but this probably isn't an objc-specific problem. If you're getting 'random' crashes from this, I doubt that just this line you posted is the problem – Louis Tur Nov 18 '15 at 00:30
  • My project is fairly large and my debugging efforts haven't given me direction about which areas might be causing the error. Can you explain how closeTree1 was able to be referenced in the debug line and then in the next line the EXC_BAD_ACCESS implies that it is nil? – jelmore Nov 18 '15 at 13:10
  • The GitHub link: https://github.com/jeromed88/JB-v-6.0 – jelmore Nov 18 '15 at 14:10
  • I took a look at your repo out of curiosity, and if I could offer some advice.. a single file with 21,500+ lines of code (i believe it's `GameScene.swift`) is going to be incredibly annoying to sift through to debug. So not only is that going to make it harder on you to navigate the code to pinpoint the problem, it's not really going to encourage folks to look at the repo either.. another potential reason you've gotten 0 replies on this question. You need to structure your code into smaller, modular classes and arrange your repo in some hierarchical structure. Organization is very important. – Louis Tur Nov 19 '15 at 16:21
  • I appreciate the advice. I would like to organize everything much more clearly, but I am fairly new and breaking up the project has proven a more onerous task than I thought. That is definitely an area I need to spend more research time on :) – jelmore Nov 20 '15 at 01:16

1 Answers1

0

I was able to figure out the issue, I'll discuss it generally since I didn't provide many details in the question.

closeTree1 was still allocated, so it was not a manual memory management or ARC problem. I was using SKAction.runBlock({functionname()}), so functionname() executed while actions were being evaluated. Long story short, this was the wrong part of the frame for this to happen, so I used runBlock to set a variable that ran the function in update() instead, and it works great now :). The takeaway seems to be that a vaguely thought out frame progression can cause EXC_BAD_ACCESS.

I was getting some very strange (and I was sure was impossible) behavior with this error. When the EXC_BAD_ACCESS error didn't come up and the program ran, sometimes the same Sprite was added to the scene twice, with both following input commands! It must have been the same Sprite because only one SKSpriteNode has that image and follows those commands. I'm not sure if this is a SpriteKit bug, but it's working reliably now.

jelmore
  • 1
  • 3