Re: Setting pointer to nil
Re: Setting pointer to nil
- Subject: Re: Setting pointer to nil
- From: Ricky Sharp <email@hidden>
- Date: Tue, 20 Dec 2005 16:50:09 -0600
On Dec 20, 2005, at 4:39 PM, Jeff LaMarche wrote:
I've noticed that with Xcode 2.2, the Core Data Application
template has been updated. The new application delegate class
template been given a new dealloc method:
- (void) dealloc
{
[managedObjectContext release], managedObjectContext = nil;
[persistentStoreCoordinator release],
persistentStoreCoordinator = nil;
[managedObjectModel release], managedObjectModel = nil;
[super dealloc];
}
I've never seen pointers set to nil like this in Objective-C. I
don't recall any of the books I've got recommending doing this in a
dealloc. I HAVE seen it done a lot in Java to blue in the garbage
collector to the fact that you're done with an object.
Now that I see it, it does seem like a good idea - it seems like
this practice would prevent sending messages to released objects,
instead just sending a harmless message to nil (although it could
potentially mask problems in some cases), but I can't help but
wondering if this isn't the first sign that Apple's planning on
giving us an Objective-C garbage collector at some point.
I do this all the time, but mainly via accessors.
For example, the above could be rewritten to be:
- (void)dealloc
{
[self setManagedObjectContext:nil];
[self setPersistentStoreCoordinator:nil];
[self setManagedObjectModel:nil];
[super dealloc];
}
The nice thing about this is that it will work with many of the
different accessor patterns. One example:
- (void)setManagedObjectContext:(id)newManagedObjectContext
{
[newManagedObjectContext retain];
[managedObjectContext release];
managedObjectContext = newManagedObjectContext;
}
It's nice to be able to leverage your setters from within dealloc.
If you use Accessorizer, check out the Dealloc group under the
Accessor style tab. You can use the [self setObject:nil] pattern
which is what I personally use most often.
___________________________________________________________
Ricky A. Sharp
mailto:email@hidden
Instant Interactive(tm)
http://www.instantinteractive.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden