• 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: publiclook <email@hidden>
  • Date: Sun, 4 May 2003 17:58:25 -0400

Cocoa's reference counted memory management really is very simple. When you understand it, you will wonder why you ever struggled. Cocoa's memory management conventions are much simpler than the conventions for creating and maintaining balanced binary trees for example ;)

To specifically answer your question:
The following code is a typical "set" accessor method:

- (void)setMainSprocket:(Sprocket *)newSprocket
{
[mainSprocket autorelease];
mainSprocket = [newSprocket retain]; /* Claim the new Sprocket. */
return;
}

It could have been written as the following:

- (void)setMainSprocket:(Sprocket *)newSprocket
{
[mainSprocket autorelease];
mainSprocket = [newSprocket copy]; /* Copy the Sprocket. */
}

Do you see now why the previous value of mainSprocket must be released or autoreleased before assigning a new value to mainSprocket ?

The purpose of the -setMainSprocket: method is to set the valie of mainSprocket to point to newSprocket and it therefore takes ownership of newSprocket. Whatever object created the newSprocked passed into the method is responsible for releasing or autoreleasing the created object. In fact, the object that created the newSprocket may release it immediately after calling -setMainSprocket: or it may have been autoreleased before -setMainSprocket: was called. Because the object that implements the -setMainSprocket: method is keeping a pointer to the newSprocket passed in, it is necessary for -setMainSprocket: to retain the argument, newSprocket, or the object that implements the -setMainSprocket: may be left with a pointer to a deallocated object immediately after the method returns. Similarly, because the previous value of mainSprocket was retained, it must be released or autoreleased to avoid a memory leak.

If Apple's documentation is not clear enough on this subject, you might consider reading Microsoft's COM documentation or many other documents. Reference counting is a common technique and is in no way unique to Apple or Cocoa.

By the way, "you" is referring to the object that is taking action and it is common to anthropomorhize in object oriented programming.



On Sunday, May 4, 2003, at 05:22 PM, 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.

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.
_______________________________________________
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: lbland <email@hidden>
References: 
 >Cocoa - Naive questions about memory (From: Danny Swarzman <email@hidden>)

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