title: Unable to locate memory leak using Xcode Instruments
body: <h2>The problem</h2>
  
  <p>I discovered earlier that my memory usage in my game was only going up when the tiles were moved, but it never went back down again. From this, I could tell there was a memory leak.</p>
  
  <p>I then started using Xcode Instruments, which I am very new to. So I followed many things from <a href="https://spin.atomicobject.com/2016/01/25/ios-memory-leak-xcode/" rel="nofollow noreferrer">this article</a>, especially the <code>Recording Options</code>, and then I set the mode to show the <code>Call Tree</code>.</p>
  
  <h2>Results of Instruments</h2>
  
  <p><a href="https://i.stack.imgur.com/6uTn4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6uTn4.png" alt="Instruments - Results of testing for leaks"></a></p>
  
  <h2>What do I need help with?</h2>
  
  <p>I have two functions that just move all the tiles along that row/column, and then clones the tile at the end (using <code>node.copy()</code>) so everything can "loopover", hence the project name.</p>
  
  <p>I feel as if the tile cloning may be causing some retention cycle, however, it is stored in the variable within the function scope. After I run the <code>SKAction</code> on the clone, I remove the tile from the scene using <code>copiedNode.removeFromParent()</code>.</p>
  
  <p>So what may be causing this memory leak? Could I be looking in the wrong place?</p>
  
  <hr>
  
  <h2>Code</h2>
  
  <p>I have shortened this code to what I consider necessary.</p>
  
  <p>Declaration at the top of the class:</p>
  
  <pre><code>/// Delegate to the game scene to reference properties.
  weak var delegate: GameScene!
  /// All the cloned tiles currently on the board.
  private var cloneTiles = [SKSpriteNode]()
  </code></pre>
  
  <p>Cloning of the tile within the moving tiles functions:</p>
  
  <pre><code>/// A duplicate of the current tile.
  let copiedNode = currentTile.node.copy() as! SKSpriteNode // Create copy
  cloneTiles.append(copiedNode) // Add as a clone
  delegate.addChild(copiedNode) // Add to the scene
  let copiedNodeAction = SKAction.moveBy(x: movementDifference, y: 0, duration: animationDuration) // Create the movement action
  
  // Run the action, and then remove itself
  copiedNode.run(copiedNodeAction) {
      self.cloneTiles.remove(at: self.cloneTiles.firstIndex(of: copiedNode)!)
      copiedNode.removeFromParent()
  }
  </code></pre>
  
  <p>Function to move tiles immediately:</p>
  
  <pre><code>/// Move all tiles to the correct location immediately.
  private func moveTilesToLocationImmediately() {
      // Remove all clone tiles
      cloneTiles.forEach { $0.removeFromParent() }
      cloneTiles.removeAll()
  
      /* Moves tiles here */
  }
  </code></pre>
  
  <p>Is there something I need to declare as a <code>weak var</code> or something? I know how retain cycles occur, but do not get why it exists in this code, as I remove the cloned tile reference from the <code>cloneTiles</code> array.</p>
  
  <hr>
  
  <h2>Roughly where the leak is occurring (helped by <code>Mark Szymczyk</code>)</h2>
  
  <p>Here is what happened after I double-clicked on the move tiles function in the call stack (refer to his answer below):</p>
  
  <p><a href="https://i.stack.imgur.com/YTOCb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YTOCb.png" alt="Instruments - Finding the memory leak"></a></p>
  
  <p>This is confirming that the memory leak is caused somehow by the node clone, but I still don't know why this node is still being retained after it is removed from the <code>cloneTiles</code> array and the scene. Could the node be having trouble getting removed from the scene for some reason?</p>
  
  <p>Please leave <em>any</em> tips or questions about this, so this problem can be solved!</p>
  
  <h2>More investigating</h2>
  
  <p>I have now been trying to get to grips with Xcode Instruments, but I am still really struggling to find this memory leak. Here is the leaks panel which may help:</p>
  
  <p><a href="https://i.stack.imgur.com/wM5Vh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wM5Vh.png" alt="Instruments - Leaks panel"></a></p>
  
  <p><a href="https://i.stack.imgur.com/WcIyf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WcIyf.png" alt="Instruments - Leak&#39;s history"></a></p>
  
  <p>Even after trying <code>[weak self]</code>, I still had no luck:</p>
  
  <p><a href="https://i.stack.imgur.com/zxKhf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zxKhf.png" alt="Instruments - Using [weak self] within the closure"></a></p>
  
  <p>Even the leaks history still looks the same with the <code>[weak self]</code> within the closure.</p>
  
  <h2>Continuing to try to resolve the reference cycle</h2>
  
  <p>Currently, <code>@matt</code> is helping me with this issue. I have changed a few lines of code, by adding things like <code>[unowned self]</code>:</p>
  
  <pre><code>// Determine if the tile will roll over
  if direction == .up &amp;&amp; movementDifference &lt; 0 || direction == .down &amp;&amp; movementDifference &gt; 0 {
      // Calculate where the clone tile should move to
      movementDifference -= rollOverDistance
  
      /// A duplicate of the current tile.
      let copiedNode = currentTile.node.copy() as! SKSpriteNode // Create copy
      cloneTiles.append(copiedNode) // Add as a clone
      delegate.addChild(copiedNode) // Add to the scene
      let copiedNodeAction = SKAction.moveBy(x: 0, y: movementDifference, duration: animationDuration) // Create the movement action
  
      // Run the action, and then remove itself
      copiedNode.run(copiedNodeAction) { [unowned self, copiedNode] in
          self.cloneTiles.remove(at: self.cloneTiles.firstIndex(of: copiedNode)!).removeFromParent()
      }
  
      // Move the original roll over tile back to the other side of the screen
      currentTile.node.position.y += rollOverDistance
  }
  
  /// The normal action to perform, moving the tile by a distance.
  let normalNodeAction = SKAction.moveBy(x: 0, y: movementDifference, duration: animationDuration) // Create the action
  currentTile.node.run(normalNodeAction) { [unowned self] in // Apply the action
      if forRow == 1 { self.animationsCount -= 1 } // Lower animation count for completion
  }
  </code></pre>
  
  <p>Unfortunately, I could not make <code>copiedNode</code> a <code>weak</code> property as it would always be instantly <code>nil</code>, and <code>unowned</code> caused a crash about the reference being read after being deallocated. Here is also the <code>Cycles &amp; Roots</code> graph if this is helpful:</p>
  
  <p><a href="https://i.stack.imgur.com/tOfHT.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tOfHT.jpg" alt="Instruments - Cycles &amp; Roots graph"></a></p>
  
  <hr>
  
  <p>Thank you for any help!</p>