Questions about destruction concept


It is very straight forward to depict the destruction concept in a chain just using the example in the book.

However, as I did the Gold challenge exercise in Chapter 2 that I put each code in the same project file, my trial about the destruction/ memory deallocation in Chapter 3 has included the exercise codes.

The build and run was successful though. The console showed that the container was destroyed and so did the corresponding BNRItem being pointed to by the container’s NSMutableArray.

Sorry for that I’m not good at drawing charts, I try my best to describe in words here my concept whether it is right or wrong.

I tried to draw some more ‘blocks’ with regards to the ‘Container’ on Figure 3.6. My thought was that the instance of BNRContainer created was pointed by the ‘main’s frame’ because it was ‘alloc’ and ‘init’ it in the main function. And followed that, the instance of BNRContainer pointed to an NSMutableArray and then points to different BNRItem instances created above.

When ‘items’ was set to nil, I thought it was in another ‘path’ of ‘ownership’ which the container shouldn’t be affected. But indeed, it was affected and all containers and BNRItems were destroyed according to the console.

I wonder if i got some concepts wrong here. Would someone could clear my mind and correct me for something wrong?

Thanks for that!


The problem with reporting with the console is that anytime you log a variable, that adds an owner temporarily. Additionally, ARC may make decisions to keep an object around longer than you expect. It’s tough to reliably determine when objects are really destroyed without using Instruments.