Beginner Question
Beginner Question
- Subject: Beginner Question
- From: James Spencer <email@hidden>
- Date: Sat, 2 Feb 2002 13:08:49 -0600
As a newcomer to Cocoa, I'm trying to work through example material in,
among other things, Learning Cocoa. In working through the To Do
example in Chapter 12, there is code for the initialization of a
subclass of NSMatrix, CalendarMatrix, which is simply a 6 X 7 array of
button cells. As I said the code is presented for initialization using
initWithFrame:frameRect which merely initializes the super class with a
prototype cell, set's the tags for the cells and then initializes an
instance variable with an NSCalendarDate which it retains.
Now, I thought I was starting to understand Cocoa's memory management
conventions but it appears I still have a long way to go. The book
tells me "Now that you've seen the initialization code, you should have
no problem implementing dealloc for Calendar Matrix." When I read tis I
agreed but having taken my shot at the code, I looked at the authors'
sample code and I find:
- (void)dealloc {
[selectedDay release];
}
This presents several questions that I'm hoping some kind soul will help
me with. I've tried finding the answers on my own in the Cocoa
documentation, by searching through the archives of this list (terrible
search engine just to add my 10 cents), and by looking through what I
could find at the O'Reilly site. (That doesn't mean that the answer
isn't right in front of me but I did look before bothering the list with
these
1) Does super's dealloc method release the individual cells in the
matrix? I would have thought so as it must be retaining them at
initialization.
2) If so, and for that matter, even if not, don't we have to make a call
to [super dealloc] to give the superclass a chance to get rid of its
structures?
3) If NSMatrix' dealloc does NOT release the individual cells, then
don't we have to loop through the matrix ourselves with something like:
for (i = 0; i < 6; i++)
{
for (j = 0; j < 7; j++)
{
[[self cellAtRow:i column:j] release];
}
}
4) With regard to the release of selectedDay (the NSCalendarDate) and,
assuming that we need to release the individual cells, their release as
well, is there a reason to use release in this context rather than
autorelease?
Thanks for the help.
Spence
"Badges?? We don't need no stinkin badges!"