Question

Here are two pieces of Objective-C code in a Foundation app. This piece of code is in a function:

    [arrayOfObjects addObject:[[TheShape alloc] init]];
    NSLog(@"%@", arrayOfObjects); // log verifies "<TheShape..." is in the array
    [arrayOfObjects release];

and in my TheShape class, I have this dealloc override method:

    - (void)dealloc {
        NSLog(@"TheShape dealloc called.");
        [super dealloc];
    }

Although my program works otherwise, it doesn't work the way I expect it to. When the [arrayOfObjects release] message is sent, I expect to see the "TheShape dealloc..." string appear in the log. It doesn't.

Q1: Why not?

So I dig a bit and simplify things. If I do something simpler like this:

    TheShape *aShape = [[TheShape alloc] init];
    [aShape release];

the debug message still doesn't appear in the log.

Q2: Why not?

But if I do this:

    TheShape *aShape = [TheShape new];
    [aShape release];

the debug message does appear in the log. The debug message also appears in the log if I change the alloc/init in the first sample to new, too.

Q3: Why?

Obviously, I'm missing something conceptual in the alloc/init/release cycle (Q's 1 and 2) and in the supposed equivalency of new and alloc/init (Q3). Can anybody point me to a tutorial that explains things a bit more for the hard of thinking, like me?

Thanks,

Bill

Was it helpful?

Solution

By any chance did you override +new on your class? It should be doing precisely the same thing as +alloc/-init.

In any case, your very first line

[arrayOfObjects addObject:[[TheShape alloc] init]];

is leaking your TheShape instance. You should turn that in to

[arrayOfObjects addObject:[[[TheShape alloc] init] autorelease]];

OTHER TIPS

This Link provides useful information on memory management.

There's no reason for the dealloc method to be called: your object still has a non-zero reference count.

Whenever you create an object, it begins its existence with a reference count of 1. Once this count reaches 0, it is deallocated. There are specific rules for memory management that help us all stay sane. These rules speak of 'object ownership'.

Basically, you become the owner of an object if you meet one of these conditions:

  • You call retain on the object;
  • You obtain an object by calling copy or mutableCopy on another one;
  • You call the alloc method on an objet.

Whenever you own an object, you are responsible for releasing once you don't need it anymore. It will be deleted only once no one needs it anymore.

In your snippet, you create the object that you pass to your array. Your array begins to use it, so it calls retain on it (so it stays alive while it's being held). However, once you release the array, your object still needs to be release by you, since you called the alloc method.


EDIT The simple snippet:

TheShape *aShape = [[TheShape alloc] init];
[aShape release];

should show the message, though. :/

There are two reasons I can see this not happening:

  • You did something weird with either the alloc, init or release method (in which case, post your code);
  • You run in a garbage-collected environment and dealloc will simply never be called (it's finalize you should use in this case). Though I don't see why +new then release would give the expected results then.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top