Re: release and reference counting query
Re: release and reference counting query
- Subject: Re: release and reference counting query
- From: mmalcolm crawford <email@hidden>
- Date: Fri, 21 Nov 2008 11:48:32 -0800
On Nov 21, 2008, at 11:15 AM, Marc Stibane wrote:
Am 19.11.2008 um 10:05 schrieb mmalcolm crawford:
On Nov 18, 2008, at 10:33 AM, Marc Stibane wrote:
Lets forget for a moment that the dealloc never get's called at
all on the iPhone
This is simply untrue.
Nope.
Try to set a breakpoint inside the dealloc in the same file as
applicationDidFinishLaunching, run your app and press the home button.
Never called, and purposely so - the application finishes anyway, so
why spend cycles deallocating small blocks one-by-one when you can
just throw away all memory the app used in one block...
I'm perfectly well aware of that optimisation; as an unconstrained
statement, however, "dealloc never get's called at all on the iPhone"
is untrue. There are many situations when dealloc *will* be called,
and so you should take that into account.
Because:
Cocoa strongly advocates you use accessor methods to set instance
variables(*);
You are discouraged from setting instance variables directly
anywhere other than in initializers and dealloc(*);
For me, applicationDidFinishLaunching IS an initializer...
You're free to hold your own views; as far as Cocoa is concerned,
however, it is *not* an initialiser.
On iPhone in particular, you should avoid the use of autorelease.
The latter point in particular means that this alternative:
self.viewController = [[[UIViewController alloc]
initWithNibName:@"MoveMeView" bundle:[NSBundle
mainBundle]] autorelease];
does not follow best practice.
Thus -- almost by a process of elimination -- you're left with the
pattern shown in iPhone samples.
I stick with my applicationDidFinishLaunching implementation - 1
line instead of 3.
Again you're welcome to do so, and you're welcome to deal with any
bugs that arise as a result.
Of cause you're right were it some other method which could be
called more than once and not applicationDidFinishLaunching, which
is only called once and - as stated above - never paired with
dealloc (however, applicationWillTerminate is called).
@interface MyObject : MySuperClass {
NSString *string;
}
@property (nonatomic, retain) NSString *string;
@end;
@implementation MyObject
@synthesize string;
- (id)init {
if (self = [super init]) {
// a poor example for several reasons, but take this just as
a way
// to crease a new, owned, object
string = [[NSString alloc] initWithFormat:@"now: %@", [NSDate
date]);
}
return self;
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
string = [[NSString alloc] initWithFormat:@"now: %@", [NSDate
date]);
}
You just leaked the first value of string.
And to preempt the assertion that "this would never happen" --
precisely this *did* happen to someone else who made exactly your
point, within a week of their having made it.
mmalc
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden