On 12 Jul 2009, at 10:16 AM, William Squires wrote:

- (void)setWebView:(SomeAppDelegate *)appDelegate
{
NSString *htmlString;

htmlString = @"<div style=\"font-family;Helvetica,Arial, sans-serif; font-size=48pt;\"align=\"center\">"; htmlString = [htmlString stringByAppendingString:appDelegate.savedNumber];
htmlString = [htmlString stringByAppendingString:@"</span>"];
[webView loadHTMLString:htmlString baseURL:nil];
// [htmlString release]; // <- is this necessary?
}

Two issues.

First, the release you've commented-out is not only unnecessary, it is wrong. The value that htmlString holds is not a literal string, but the computed result of the method -[NSString stringByAppendingString:]. By the rules, you don't have ownership of that object, and must not release it.

(You're balancing a <div> with a </span>, but I assume that's just a typed-in-mail typo.)

Second, your nominal question about retaining/releasing literal strings. In practice, you don't have to use the ownership pattern on them. They are kept as constant objects in the binary, and memory- management methods on them are no-ops. I confess I take advantage of this in my own code.

I really shouldn't. Code that starts with the assumption that I don't have to manage them will fail when I change it to use strings that have to be managed. If the memory-management methods are no-ops on literals, they also do no harm. Therefore I should do:

myInstanceVariable = [@"literal" copy];
...
[myInstanceVariable release];

-or-

@property(copy) NSString *  myProperty;
...
self.myProperty = @"literal";
...
self.myProperty = nil;  // Pace the debate on whether
                        // and when this should be
                        // release-and-set-nil.

Now, don't get nuts over this. You shouldn't retain/copy a literal string in contexts where you wouldn't do the same for any other object.

        — F

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to