Listing 49-1you have coded two classes, Project and Task, each of which have deinit methods that will execute right before these instances are disposed of.
Note Instead of a delegate property in Task, you have a property named parent, which is an optional Project type that a Task instance may use to get information about the project that the Task instance belongs to. Finally, when you instantiate your Project and Task instances, you declare the Project instance as optional. You declare Project as optional because you need to set this instance to nil at the end to dispose of the instance.
When you build and run the application in Listing 49-1, it seems to run fine. But, if you look at the console log, you will notice that no messages were written out even though you expected some. This means the code in deinit was never reached and these five Task instances were never disposed of. This is a memory leak.
You could solve this problem by removing the line of code that assigns the Project instance to the parent property for each Task instance (Listing 49-2).
Listing 49-2. Removing strong Reference
var p:Project? = Project0 pi.name = "Cook Dinner"
let taskNames = ["Choose Menu", "Buy Groceries", "Prepare Ingredients", "Cook Food"]
for name in taskNames{ var t = Task() t.name = name //t.parent = p p!.listOfTasks.append(t)
}
p = nil
When you comment out the line from Listing 49-2, you will get the following output in the console log:
Cook Dinner project is being deinitialized Choose Menu task is being deinitialized Buy Groceries task is being deinitialized Prepare Ingredients task is being deinitialized Cook Food task is being deinitialized