Memory Leak – Subclassing UIViewController

Here’s a summary of what I’ve discovered for one of the memory leak issues that I’ve encountered. I believe this is a bug of some type in either Instruments or iOS itself.

In the code below, I create a subclass of UIViewController named MemoryLeakViewController, and then I change MealTableViewController to inherit from this new class. Next, I create an OperationQueue instance variable, and use the the queue to print a message in the tableView(_:cellForRowAt:) method.

The MemoryLeakViewController is completely empty, and the block executed in the OperationQueue doesn’t reference anything. I have no idea why a memory leak is reported. I don’t think what I’m doing here is anything crazy, but I wouldn’t mind another pair of eyes if anyone out there wants to try this. I’ve wasted many hours trying to track this and strip it down to the most simple example.

Steps to reproduce:

  1. Download the sample code from Start Developing iOS Apps (Swift): Create a Table View. This is a safe website from Apple itself. The Download File link at the bottom of the page is where you’ll find the “06_CreateATableView.zip” file.
  2. Unzip the file and open it in Xcode. I’m working with version 8.2.1 (8C1002).
  3. Replace the contents of the MealTableViewController class with the source code below.
  4. Run the app with Instruments on a device. I’m using an iPhone 5s with iOS 10.2.1. Note: this must be executed on a device because it doesn’t happen in the iOS Simulator.
  5. Bang! The memory leaks are indicated in the screenshot at the end of this post.

https://gist.github.com/spcoder/e0e85784c33fc0c10bcb25bcf3e41fc3

screen-shot-2017-01-31-at-6-57-08-am

Leave a comment