Cocoa - Naive questions about memory
Cocoa - Naive questions about memory
- Subject: Cocoa - Naive questions about memory
- From: Danny Swarzman <email@hidden>
- Date: Sun, 4 May 2003 14:22:50 -0700
Forgive me if I'm asking something that has been asked here before. If that
is the case, please give me enough information so that I can easily search
for it. Today, Apple's searching is broken so URLs are really nice to get.
I've read the "Object Ownership and Disposal" document and the chapter from
the O'Reilly book and still don't
understand how memory works with Cocoa in Objective-C.
In both documents much use is made of the personal pronoun 'you'. Is 'you'
a programmer? a method? an object?
a class?
'You' is the owner of an object. Is this a relationship embedded in data
structures maintained in the mind of the
programmer?
Is 'the end of the event cycle' when a program completes one time through
the loop or when the program exits?
In "Object Ownership and Disposal" we see:
Cocoa therefore sets this policy: If you create an object (using alloc,
allocWithZone:, or new) or copy an object
(using copy, copyWithZone:, mutableCopy,or mutableCopyWithZone:), you alone
are responsible for releasing it.
If you did not directly create or copy the object, you do not own it and
should not release it.
Then there is sample code that appears to do the opposite:
- (void)setMainSprocket:(Sprocket *)newSprocket
{
[mainSprocket autorelease];
mainSprocket = [newSprocket retain]; /* Claim the new Sprocket. */
return;
}
Why is it necessary to call autorelease for he object to which mainSprocket
pointed before setMainSprocket is
invoked. If there is another pointer to that object in another method,
won't that there be a call in that other method
to release the object using the other pointer. If there isn't another
pointer, by what mechanism could the memory
management system be able to dectect that the object is no longer used?
If someone can point me to a web site or document that explains this stuff
in other terms I would be grateful. I
would particularly appreciate documentation free of anthropomorhism.
Thanks.
-Danny Swarzman
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.