Q:

iphone - autoreleasing a CGContextRef?

I have this method
-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path
{

   // bla bla
   // at some point I have

   CGContextRef myOutContext = CGPDFContextCreate (dataConsumer, &inMediaBox, NULL);

   // and then I have to return the value
    return myOutContext;
}

The problem is that myOutContext was not released and will leak.
if I put a CGContextRelease (myOutContext) before the return, then the reference will arrive invalid at the caller...
How do I solve that? Doing return [myOutContext autorelease];
will not work, as I suspected and tested.
thanks.

A:

Because your method has "Create" in its name, it's expected that the caller will be responsible for releasing the object. See http://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/uid/20001148-103029.

Update: Because this is a method and not a function, you should change it to start with "new" as described at http://developer.apple.com/library/ios/documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-SW1.

