Re: Newbie Memory Questions on a Code Fragment
Re: Newbie Memory Questions on a Code Fragment
- Subject: Re: Newbie Memory Questions on a Code Fragment
- From: Kenneth Bruno II <email@hidden>
- Date: Wed, 21 Jan 2009 10:42:49 -0500
On Jan 21, 2009, at 9:07 AM, Kenneth Bruno II wrote:
NSDateFormatter *aFormatter = [NSDateFormatter new];
NSArray *listOfMonthNames = [[aFormatter standaloneMonthSymbols]
retain];
One small error here, I should have added this line after these two in
order to properly release the NSDateFormatter:
[aFormatter release];
This needs to be done because I created a new NSDateFormatter which
has a retain count of 1. Since I don't need it past this point I
should release it. It's a small memory leak if I don't but it's a
good idea to always properly clean up after yourself. Instead of
adding an additional line to release I could have also just changed
the first line to this:
NSDateFormatter *aFormatter = [[NSDateFormatter new] autorelease];
The advantage to using autorelease is that I now don't need to
remember to release later and it puts the allocation and cleanup all
in one line, producing cleaner code. The disadvantage is that
autoreleasing takes up a bit more time and memory than just releasing
the object. In this case that's negligible, only worry about it if
you are creating and autoreleasing tons of objects in time or memory-
critical code.
_______________________________________________
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