Questions about retain
Questions about retain
- Subject: Questions about retain
- From: Ted Lowery <email@hidden>
- Date: Fri, 27 Dec 2002 13:12:04 -0500
Hi all-
Here's some code that doesn't work reliably:
NSString* coords = [NSString stringWithFormat:@"%0.0f, %0.0f,
%0.0f, %0.0f",
r.origin.x, r.origin.y, r.size.width, r.size.height];
body = [body stringByAppendingString:coords];
NSString* s = [NSString stringWithString:header];
s = [s stringByAppendingString:body];
[textView setString:s];
However, change the body = line to the below fixes the problem, or at
least fixes the crash :-).
body = [[body stringByAppendingString:coords] retain];
Header and body begin life elsewhere...
header = @"<header>\n";
body = [NSString string];
This seems as though I am unnecessarily increasing the retain count on
body so that it will never be freed.
I tried retaining coords, thinking that was getting dealloc at the end
of the routine, but this was not the answer. My first attempt I
retained nothing, because I assumed that stringByAppendingString was
making a COPY of the string, and adding it to body, which was already
retained.
Do I need to capture body before the append in a separate variable, and
then release that afterward?
Thanks for the comments. I'd investigate the behavior in MallocDebug,
but as seen in previous posts, I cannot get that to work (on my
machine. :-)
Cheers, Ted
_______________________________________________
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.