Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Crash when calling custom methods in custom subclass.




On Jul 30, 2005, at 1:01 PM, mmalcolm crawford wrote:

> - (void) setContent: (NSString *)newContent
> {
> [_content release];
> _content = [newContent copy];
> }
It's not clear why you're not following the implementation pattern clearly set out in the documentation and in other references?


The closest the documentation gets to this pattern would be:

- (void) setContent: (NSString *)newContent
{
    [_content autorelease];
    _content = [newContent copy];
}

Note, autorelease, not release.

The document I referred to in my previous reply, however, illustrates the appropriate implementations of accessor methods for a collection:

<http://developer.apple.com/documentation/Cocoa/Conceptual/ ModelObjects/Articles/moAccessorMethods.html>

If (as appears to be the case) you intend that the array be mutable, note the use of 'mutableCopy':

- (void)setEmployees:(NSMutableArray *)newEmployees

{
    if (employees != newEmployees) {
        [employees autorelease];
        employees = [newEmployees mutableCopy];
    }
}


Unfortunately, I have tried learning from books, but find that I learn much better by analyzing examples. While I will look for the book you've suggested, if anyone could point out what I've done wrong, and perhaps provide me with an example of a proper setter, I'd greatly appreciate it.

There are clearly limitations to this approach. Im particular, given examples of proper setters, you should ensure that you follow them. The rules for memory management are not difficult, it is worth the investment of time in leaning them -- see, for example:


<http://developer.apple.com/documentation/Cocoa/Conceptual/ MemoryMgmt/Concepts/ObjectOwnership.html>

mmalc

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Crash when calling custom methods in custom subclass. (From: Adam Raney <email@hidden>)
 >Re: Crash when calling custom methods in custom subclass. (From: Charilaos Skiadas <email@hidden>)
 >Re: Crash when calling custom methods in custom subclass. (From: Adam Raney <email@hidden>)
 >Re: Crash when calling custom methods in custom subclass. (From: Adam Raney <email@hidden>)
 >Re: Crash when calling custom methods in custom subclass. (From: mmalcolm crawford <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.