Graphics Programs Reference
In-Depth Information
In
main.m
, after you finish printing out the array of
BNRItem
s, you set the
items
vari-
able to
nil
. Setting
items
to
nil
causes the array to lose its only owner, so that array is
destroyed.
But it doesn't stop there. When the
NSMutableArray
is destroyed, all of its pointers to
BNRItem
s are destroyed. Once these variables are gone, no one owns any of the
BNRItem
s, so they are all destroyed. Destroying a
BNRItem
destroys its instance vari-
ables, which leaves the objects pointed to by those variables unowned. So they get des-
troyed, too.
Let's add some code so that we can see this destruction as it happens.
NSObject
imple-
ments a
dealloc
method, which is sent to an object when it is about to be destroyed. We
can override this method in
BNRItem
to print something to the console when a
BNRItem
is destroyed. In
RandomPossessions.xcodeproj
, open
BNRItem.m
and override
dealloc
.
- (void)dealloc
{
NSLog(@"Destroyed: %@", self);
}
In
main.m
, add the following line of code.