Re: [Newbie Q] Memory Management in Cocoa
Re: [Newbie Q] Memory Management in Cocoa
- Subject: Re: [Newbie Q] Memory Management in Cocoa
- From: Bill Cheeseman <email@hidden>
- Date: Tue, 05 Nov 2002 14:21:35 -0500
on 02-11-05 1:22 PM, Flemming Bengtsson at email@hidden wrote:
>
I'm getting a little bit confused about when to dealloc an instance.
>
It's obvious that instances created like [[foo alloc] init ]; should be
>
deallocated when done with.
>
But what about instances created like:
>
NSString * aNSString = [NSString stringWithCString:"Hello" length:5];
I'm feeling mellow and talkative today, so I'll take this one on.
You should say "released," not "deallocated." A class's dealloc method might
or might not release its instance variables when it's called, and it might
or might not do other things, too, but it probably won't release itself. And
you generally won't call a class's dealloc method yourself; you'll just
write it, and Cocoa will call it for you when the right time arrives -- say,
when you release it and, as a result, its retain count changes to 0.
But that's an answer to a question you didn't ask.
Memory management confusion is very common. I've found that the only way to
really master the subject is practice. (This is not to suggest that I've
mastered it, but only that I am still practicing.) But it is very helpful to
do a little reading. This question is asked and answered frequently on these
mailing lists, so go to the mamasam Web site and peruse the archives for
topics like "retain", "release", "autorelease", and "memory management".
Better yet, buy one of the good Cocoa books, or go to any of the good Cocoa
Web sites, such as Stepwise, and read up on the subject. Stepwise, for
example, has several good articles on the subject, including a recent one or
two by mmalcolm crawford that more or less summarize Apple's current
official views on the subject.
Don't be depressed if many experts tell you that the rules are simple and
easy to understand. They are, but only after you've mastered them, so this
response isn't particularly helpful for beginners. There are many fine
points about Cocoa memory management that aren't obvious from reading about
the basic rules, in part because the basic rules assume you already know
more things than you do know about other aspects of Cocoa that have
implications for memory management.
So, the basic rules say (1) you must sooner or later release an object that
you own, and (2) you own an object that you introduced into your application
by calling alloc, allocWithZone:, copy, or copyWithZone: yourself (I'm
leaving something out here; what is it?). When you introduced your string by
calling NSString's stringWithCString: "convenience" method, you didn't call
one of those special methods yourself, so you don't have to (and shouldn't)
release it. Convenience methods create the object for you and return them
"autoreleased," which means Cocoa will release them for you at an
appropriate time. That's why they're called "convenience" methods, I guess.
By the way, stringWithCString: was recently declared to be dangerous because
it makes unwarranted assumptions about encoding, if I'm not mistaken, but
that's an answer to another question you didn't ask.
I welcome corrections of anything I've said that's really stupid.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
Croquet Club of Vermont -
http://members.valley.net/croquetvermont
_______________________________________________
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.