Re: Odd memory issue -- not a newbie
Re: Odd memory issue -- not a newbie
- Subject: Re: Odd memory issue -- not a newbie
- From: Jonathan Jackel <email@hidden>
- Date: Sun, 14 Dec 2003 21:57:40 -0500
On Dec 14, 2003, at 3:28 AM, Dietrich Epp wrote:
>
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.
I know you are trying to help, and I thank you for trying, but you've
made a some errors (one of them serious) here that I don't want to let
go uncorrected. Sorry if this sounds a little bitchy, but ...
First, I'm trying very hard NOT to make a copy so I don't suck up more
memory than necessary. That's what I said in my original post. I am,
at this point, making a duplicate because it works, not because it's
best.
Second, NSTextStorage is ALWAYS mutable. It's a subclass of
NSMutableAttributedString. So -copy cannot be implemented as -retain.
Third, since I want a text storage object, which is always mutable, the
right thing to do is -mutableCopy, not -copy. You risk a crash or
"does not respond to selector" or "trying to perform mutable method on
immutable object" warning otherwise.
Fourth, according to the docs, NSTextStorage does not conform to the
NSCopying or NSMutableCopying protocols. Its superclass does, but I'm
wary of using either -copy or -mutableCopy on an NSTextStorage.
Fifth (the serious one), your proposed release/copy setter has a flaw.
What if newStorage is the same object as myStorage? You'll release
that object before you can do the assignment, and you'll probably end
up with nil. Autorelease works, because it delays the actual release.
Release doesn't.
Sixth, your setter does the about same thing as the setter I have,
which I described in my opening post (but you snipped out):
- (void)setMyStorage:(NSTextStorage *)newStorage
{
[myStorage autorelease];
myStorage = [[NSTextStorage alloc]
initWithAttributedString:newStorage];
}
This works, but it virtually duplicates newStorage and doubles the
amount of memory needed. I'd like to just retain newStorage while I'm
using it, and release it when I'm done, but that doesn't work. Anyone
know why?
Jonathan
_______________________________________________
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.