Assuming, the following declaration for class A
@property(nonatomic, assign) DoublyLinkedList *doublyLinkedList;
, that as part of init, initialized the object
- (id)init {
self = [super init];
if (self) {
doublyLinkedList = [[DoublyLinkedList alloc] init];
}
return self;
}
and that a method
- (DoublyLinkedList*) doSomethingAndReturn {
that ultimately
return doublyLinkedList;
Does class A owns the doublyLinkedList after the return?
EDIT:
initadded withallocYou are not calling
retainon it, but ininityou are callingallocon it, so it does have a retain count of 1 — you own it and you should release it indealloc.You could simply
allocit and release it indealloc. The caller of the property can choose whether to retain. Another option would be to create the object ininit, autorelease it and then assign it to the property with(retain)instead of(assign). That way, if other places in the codeallocand assign to that property, the object youalloc‘d will get released. Then indealloc, what it’s currently assigned to will get released.Yet another option if you don’t want others to set it would be to have a
(readonly)property and a_doubleLinkedListiVar and then@synthesize doublyLinkedList = _doubleLinkedList. Then you can allocate it once ininitand know that no one else will assign it, and then release it indealloc.A good analogy is that when you retain, you’re putting a leash on it. Multiple items can put a leash on that object. It is freed only when everyone has taken the leash off.
A good guide to read:
Apple’s Memory Management Programming Guide
Specifically from that doc, these rules help: