Releasing allocated objects


I’m a bit confused. Under retain rules it clearly states if an object is created using alloc, new, or copy that we have ownership of that object. The object will not be dealloacted in the autopool release. So when do we release those? And if it is important to release objects you own to free up memory, why are there no programming examples to deal with these allocated objects?


The rule is simple: you release them when you no longer need them.

The programmer will and should know when an object is no longer needed.

// We need a Foo
Foo *foo = [[Foo alloc] init];

// We need a new foo; don't need the previous one
if (!foo)
     [foo release];
foo = [[Foo alloc] init];


That’s what I thought but I was confused as to why we aren’t being instructed to release our allocated objects at the end of our programs. Thank you again!


Because all our programs are using ARC (If you have turned it on in build settings and when you created the project). ARC does all the clean up and object managing that we would normally do. It saves us a lot of work.

Unless you’re using xCode 4.1 - you don’t need to worry about memory management in this sense - ARC does it for you. Unless you create a retain cycle - then you need to help ARC by sorting that out yourself with __weak references when creating parent - child objects.