• 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: Retain, then autorelease and then release does what?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Retain, then autorelease and then release does what?


  • Subject: Re: Retain, then autorelease and then release does what?
  • From: Ondra Cada <email@hidden>
  • Date: Sat, 4 May 2002 14:01:24 +0200

On Saturday, May 4, 2002, at 08:39 , Joel Rosenblum wrote:

This seems odd. It would make sense to retain it, then autorelease it at the end of the method, but if you retain and autorelease it at the same time, isn't that having zero net effect, thus allowing the subsequent release to release the object as it would without this code?

Nope. The pattern

[[foo retain] autorelease]

is quite a handy one, since it would postpone releasing to the moment when the active pool is released (normally at the end of the current event).

Also, I'd like a bit of clarification on one point: if I have a property of a class which the class retains, when the class is released, will all of its objects automatically be released as well, or must I explicitly release each of them at that time?

I think you meant an *instance*, not a *class*. Though class can retain anything, it is somewhat rare, and a class is never released.

What you -- I guess -- meant though was that if an instance "owns" any other object -- by creating it or by sending it retain -- it has to release it:

@interface Foo { id a,b,c; } @end
@implementation Foo
-init {
...
a=[[Whatever alloc] init];
...
}
-someMethod {
...
if (!b) b=[someobject retain];
if (!c) c=[otherobject copy];
...
}
// then you HAVE TO do this (lest you have leaks)
-(void)dealloc {
[a release];
[b release];
[c release];
[super dealloc];
}
@end
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
2K Development: email@hidden http://www.2kdevelopment.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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.

References: 
 >Retain, then autorelease and then release does what? (From: Joel Rosenblum <email@hidden>)

  • Prev by Date: Re: Accessing window in document applications
  • Next by Date: Re: Retain, then autorelease and then release does what?
  • Previous by thread: Re: Retain, then autorelease and then release does what?
  • Next by thread: Re: Retain, then autorelease and then release does what?
  • Index(es):
    • Date
    • Thread