Re: Odd memory issue -- not a newbie
Re: Odd memory issue -- not a newbie
- Subject: Re: Odd memory issue -- not a newbie
- From: Dietrich Epp <email@hidden>
- Date: Sun, 14 Dec 2003 00:28:03 -0800
On Dec 13, 2003, at 4:57 PM, Jonathan Jackel wrote:
I'm printing a text view in a special way, so I use a custom view.
Mainly, I'm interested in the text view's text storage, because that's
what I want to print. I tried to write my setter this way:
- (void)setMyStorage:(NSTextStorage *)newStorage
{
[newStorage retain];
[myStorage release];
myStorage = newStorage;
}
[snip]
Have you tried using:
- (void)setMyStorage:(NSTextStorage *)newStorage {
[myStorage release];
myStorage = [newStorage copy];
}
If the new storage object is immutable, then this is implemented as a
-retain and you don't get any more overhead. If it is mutable, then
you get a copy and you're okay if whatever is calling your function
modifies it, such as if it's an object wrapping an internal buffer used
for reading input.
In general, if you don't want a mutable object, always use -copy
instead of -retain, and if you make a new immutable class, make -copy
call -retain.
_______________________________________________
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.