Everything i've read about objective-c memory management makes it sound incredible easy. To paraphrase everyone: "release whatever you alloc, retain or copy". But I think there are some more specific cases that are not so clear cut. Below are some sample situations. What is the correct procedure for each:
SITUATION #1:
Foo *foo = [[foo alloc] init];
Foo *fooToo = [[[foo alloc] init] autorelease];
foo = fooToo;
does foo need to be released before it is assigned to fooToo?
SITUATION #2 I seem to get a lot of crashes when I do things like: (.h and .m mashed together for convenience)
Foo *foo;
@property (nonatomic, retain) Foo *foo;
@synthesize foo;
-(id)init{
self = [super init];
}
-(void)dealloc{
[super dealloc];
[foo release];
}
I'm constantly told "always release in dealloc what you set a retain property for". But this will crash, if done like I've shown.
SITUATION #3 A similar situation that will also crash: (.h and .m mashed together for convenience)
Foo *foo;
@property (nonatomic, retain) Foo *foo;
@synthesize foo;
-(id)init{
self = [super init];
self.foo = nil;
}
-(void)dealloc{
[super dealloc];
[foo release];
}
for some reason when my code makes it down to dealloc, foo isn't == nil.
SITUATION #4 Finally, just a question, what general thought process do people use when deciding between
self.foo = bar;
and
foo = bar;
when foo is declared the same as in the above cases? Is self.foo
just another way of coding:
foo = [bar retain]
;