Re: strange problem with NSOutlineView
Re: strange problem with NSOutlineView
- Subject: Re: strange problem with NSOutlineView
- From: Chris Hanson <email@hidden>
- Date: Fri, 3 Oct 2003 08:08:53 -0500
On Friday, October 3, 2003, at 01:35 AM, Nikolay Ilduganov wrote:
- (id)init
{
self = [super init];
Bad style; you should wrap the entire thing in a "if (self = [super
init]) { ... }" block, because your superclass might decide to fail
initialization.
names = [NSMutableArray arrayWithCapacity: 1];
This is incorrect; you're creating an autoreleased instance of
NSMutableArray here, and then keeping it around. You need to either
retain it or use +alloc/-initWithCapacity: instead.
- (void)dealloc
{
datasource = 0;
Should be datasource = nil; if datasource is an object. nil and 0
might be the same value, but they have different meanings.
[names release];
You're releasing an object that's probably already been released
because you created it with +arrayWithCapacity:, which adds the object
to the current autorelease pool.
- (void)addName:(char *)name
{
NSString *bsd_name = [NSString stringWithCString:name];
Don't use any of the plain C-string methods that don't take an
encoding. They're deprecated, because the string encoding they use
isn't guaranteed to be consistent across all internationalized versions
of Mac OS X. Instead, always use one of the methods that specifies a
string encoding, or always use the same string encoding (for instance,
UTF-8).
Also, bad style; Objective-C and Smalltalk code typically isn't
infested with internal underbars.
[bsd_name retain]; // why???
Because you don't understand the Cocoa memory management rules. See
one of the articles on Stepwise explaining the rules. The short
version is that by using one of the convenience constructors to create
bsd_name, you created an autoreleased object.
This is actually OK in this particular instance, because objects are
retained when they're added to collections:
[names addObject:bsd_name];
However, because the object referred to by "names" was created
autoreleased, it has probably already been released. Which means it's
probably coincidence that your code works at all.
Fundamentally, the most important thing new Cocoa developers can do
(preferably well before they ever start working on production code) is
take some time to understand the memory management model. There are a
number of good articles that explain it on the web, particularly at
Stepwise.
-- Chris
--
Chris Hanson, bDistributed.com, Inc. | Email: email@hidden
Custom Mac OS X Development | Phone: +1-847-372-3955
http://bdistributed.com/ | Fax: +1-847-589-3738
http://bdistributed.com/Articles/ | Personal Email: email@hidden
_______________________________________________
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.