• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSStrings obtained from CFStrings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSStrings obtained from CFStrings


  • Subject: Re: NSStrings obtained from CFStrings
  • From: Steven Burr <email@hidden>
  • Date: Thu, 3 Apr 2003 23:44:23 -0700

According to the Core Foundation reference:

If you receive an object from any Core Foundation function other than a creation or copy function, such as a Get function, you cannot be certain of the objects life span. For example, if the objects containing or owning object is deallocated the contained object is deallocated too. To ensure the persistence of such an object you can retain it (using the CFRetain function) or, in some cases, copy it.

http://developer.apple.com/techpubs/macosx/CoreFoundation/ ProgrammingTopics/CFMemoryMgmt/index.html

So what you present here appears to be the right approach. Since you're "getting" a CFStringRef and not "creating" or "copying" one, you theoretically cannot be certain of its life span. So you retain it to make sure it survives "dosomething" but autorelease it to make sure its reference count goes back to one at the end of the current event loop.

I added "theoretically," because in this particular case, I think you can be certain that the containing object, a CFXMLNodeRef, will survive until the NSString returned by dosomething is copied by your setter method. But it's still probably correct to code dosomething as you have as a precaution.

I believe another valid way to code it would be:

-(NSString *)dosomething
{
NSString *string = (NSString *)CFRetain(CFXMLNodeGetString(blah));
return [string autorelease];
}

On Thursday, April 3, 2003, at 05:34 PM, "ROSE,ROBERT W (HP-Corvallis,ex1)" <email@hidden> wrote:

Right, but what about getting CFStrings? Are they any different?

I did some testing yesterday evening... After I got the CFString I did a
CFGetRefCount() on it, and the return value was 1. This helps, but I still
don't know if the string was marked for "autorelease" or not.

Is this code "correct"?

-(NSString *)dosomething
{
NSString *string = (NSString *) CFXMLNodeGetString(blah);
return [[string retain] autorelease];
}

-(void)setString:(NSString *)newstring
{
if(newstring != string) {
[string release];
string = [newstring copy];
}
}

[self setString: [theobject dosomething]];
_______________________________________________
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.

  • Prev by Date: Re: resizing cells with NSBrowser (j o a r)
  • Next by Date: Re: Using NSUserDefaults from a bundle-less app
  • Previous by thread: Re: NSStrings obtained from CFStrings
  • Next by thread: stringByTrimmingCharactersInSet: Bug
  • Index(es):
    • Date
    • Thread