• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Cocoa - Naive questions about memory
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cocoa - Naive questions about memory


  • Subject: Re: Cocoa - Naive questions about memory
  • From: j o a r <email@hidden>
  • Date: Sun, 4 May 2003 23:47:12 +0200

On Sunday, May 4, 2003, at 23:22 Europe/Stockholm, Danny Swarzman wrote:

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.

You should always use:

<http://cocoa.mamasam.com/>

In both documents much use is made of the personal pronoun 'you'. Is 'you'
a programmer? a method? an object?
a class?

Couldn't say without a direct quote from that book as I don't have mine available right now.

'You' is the owner of an object. Is this a relationship embedded in data
structures maintained in the mind of the
programmer?

A bit of both. If you have an object that has a string instance variable, the object owns the string, but you as a programmer needs to remember to instruct the object to release it when it is deallocated.

Is 'the end of the event cycle' when a program completes one time through
the loop or when the program exits?

That would be after each pass through the event loop that is initiated for each new event, for example a mouse click or a key down. Cocoa gracefully allow more than one event per time you run an applications, so it would not be at the time the program exits... :)
Since you are referring to the time the default autorelease pool is deallocated, I'd also like to point out that you can create autorelease pools of your own, should the need arise. Disregard that last comment if you don't feel ready for it.

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;
}

You notice that it sayd "or new" in the policy statement above, didn't you?
In any case, that was an unfortunate example. "new" is a shorthand for alloc+init that you should generally avoid to use.

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?

Cocoa uses retain counting, a mostly manually way to deal with the disposal of object instances. Nothing will attempt to detect if an object instance is currently referenced from some other object and then automatically deallocate it. You need to learn the memory management rules of Cocoa and adopt a coding style that helps you to do the right thing automatically as you work.
This is often something that newbies complains about, but the memory management rules and practices of Cocoa are really easy to learn and once you know how they work you almost never make mistakes. Don't despair. Expect to have to learn a few new things when you learn a new language and a new framework.

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.

Anthropomor-what? Whatever. Besides Apple's own docs, you can find good things to read here:

<http://www.stepwise.com/StartingPoint/Cocoa.html>

j o a r
_______________________________________________
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.

  • Follow-Ups:
    • Re: Cocoa - Naive questions about memory
      • From: j o a r <email@hidden>
References: 
 >Cocoa - Naive questions about memory (From: Danny Swarzman <email@hidden>)

  • Prev by Date: Factory AutoRelease
  • Next by Date: Re: Cocoa - Naive questions about memory
  • Previous by thread: Cocoa - Naive questions about memory
  • Next by thread: Re: Cocoa - Naive questions about memory
  • Index(es):
    • Date
    • Thread