Reputation: 11686
I have taken to the following code which will call CFRelease after a delay. I have included it in a method for context.
CGPathRef NewPathWithRoundRect(CGRect rect, CGFloat cornerRadius)
{
// other work to create ref which is a CGPath
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
CFRelease(ref);
});
}
In the latest version of Xcode I get a warning about not releasing memory properly. The caller takes the value, uses it and then calls CFRelease. I now CFRetain it and also CFRelease it so in order to get the reference count down to zero I am setting this dispatch to run and clean up the reference.
I am not comfortable with this approach. Is there is a better pattern for returning CF objects that were created in the method and need to be returned? Should I create the path outside the method and pass it in as a parameter so it is created and released by the caller?
I have also changed my code to call CGPathRelease which is a little safer. Any suggestions are appreciated.
Upvotes: 0
Views: 337
Reputation: 49034
The CoreFoundation policy is that if a function has the word "Create" in it, then the function returns an owning (or retained) object, which the caller is responsible for releasing. So your NewPathWithRoundRect
method should be named CreatePathWithRoundRect
, and then just drop the delayed CFRelease. The caller will be responsible for releasing it when they're done with it.
Upvotes: 1