FROM : mmalcolm crawford
DATE : Sat Dec 11 11:15:51 2004
On Dec 10, 2004, at 11:08 PM, Larry Fransson wrote:
> Another helpful tip:
>
>> - (void) setWord:(NSString *)newword {
>> word = newword;
>> }
> That will leak memory every time you call it. It should be written
> more like this:
>
Umm, no, it doesn't leak memory, it simply reassigns the pointer. What
is important is that it doesn't retain new word. Thus if and when
newword is deallocated the reference becomes invalid, typically with
unpleasant consequences...
> - (void)setWord:(NSString *)newWord
> {
> [newWord retain];
> [word release];
> word = newWord;
> }
>
For "property list" attributes, (to ensure proper encapsulation) you're
usually recommended to copy rather than retain the new value -- see for
example:
<http://www.stepwise.com/Articles/Technical/2002-06-11.01.html>
mmalc
DATE : Sat Dec 11 11:15:51 2004
On Dec 10, 2004, at 11:08 PM, Larry Fransson wrote:
> Another helpful tip:
>
>> - (void) setWord:(NSString *)newword {
>> word = newword;
>> }
> That will leak memory every time you call it. It should be written
> more like this:
>
Umm, no, it doesn't leak memory, it simply reassigns the pointer. What
is important is that it doesn't retain new word. Thus if and when
newword is deallocated the reference becomes invalid, typically with
unpleasant consequences...
> - (void)setWord:(NSString *)newWord
> {
> [newWord retain];
> [word release];
> word = newWord;
> }
>
For "property list" attributes, (to ensure proper encapsulation) you're
usually recommended to copy rather than retain the new value -- see for
example:
<http://www.stepwise.com/Articles/Technical/2002-06-11.01.html>
mmalc
| Related mails | Author | Date |
|---|---|---|
| Apirak | Dec 11, 05:14 | |
| Apirak | Dec 11, 06:40 | |
| Larry Fransson | Dec 11, 08:08 | |
| mmalcolm crawford | Dec 11, 11:15 | |
| mmalcolm crawford | Dec 11, 11:23 |






Cocoa mail archive

