Hey all, this is something I’ve wondered about for a while and never really figured out. If I allocate and initialize another class’s instance variable/property (example below), am i responsible for releasing it?
In Foo, I have an instance of Bar (called bar) and want to init one of Bar’s variables like so:
self.bar.variable1 = [[UIBarButtonItem alloc] initWithCustomView:customView];
Foo is responsible for releasing the UIBarButtonItem it creates because Foo owns it. This can be done simply by sending an
autoreleasemessage to theUIBarButtonItem. Otherwise, this will leak.If Bar needs to keep
variable1around, it must claim ownership of theUIBarButtonItem. Bar is responsible for retaining (and then releasing at some point in the future) theUIBarButtonItemitself.