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 12:27:32 -0500
On Jan 21, 2009, at 12:04 PM, mmalc Crawford wrote:
On Jan 21, 2009, at 7:07 AM, Kenneth Bruno II wrote:
When I no longer need it I'll just call release on the object, this
would most likely be done in the dealloc method of the class in
which I initialized my object.
As a general statement, this is at best misleading.
If you maintain an object as an instance variable, you're expected
to provide accessor methods to retain and release it as well.
Not necessarily, the object could be purely for internal use in the
class in which case you wouldn't provide accessor methods. I made the
general statement simply because I wanted to emphasize that you should
only call release when you want to end your ownership of the object,
for whatever reason. It's up to the programmer to decide when that is
appropriate.
You should only call release on objects that you have created
through methods that begin with alloc, new, or copy or objects that
you have previously called retain upon. I suggest that you review
the Cocoa Memory Management Rules to understand this further:
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html
>
If you review that, you will find that your summary contains a small
but important omission. This is why it's typically better simply to
point to the Guide than to try to summarise yourself.
Which is exactly why I linked to it and suggested that he read it
instead of just relying on my summary.
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.
"Which you don't own..."
Chasing retain counts is one of the surest ways of quickly becoming
confused when trying to track down memory management problems.
I wouldn't ever advocate chasing retain counts, I was simply pointing
out the fact that the object will stay around because I retained it.
I probably should have said "has been retained by me and must be
released" or something similar.
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.
It's not clear why this is simply a "good idea"? You should not
allow your program to leak memory. Even a "small" memory leak on an
iPhone application can become a terminal problem if the code is
called often enough.
Yes, memory leaks are a bad idea and should be taken care of - that's
why I bothered to make a second post correcting my error. I guess I
could have said it more strongly but I think the point was made, avoid
memory leaks.
_______________________________________________
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