iphone . ending, closing and releasing a CGContext
<p>I have a method for creating a PDF on iPhone.</p>

<p>The method begins like this</p>

<pre><code>CGContextRef pdfContext = [self createPDFContext...]
// this line creates a CGContextRef that I will use to write the page
// if not released this will leak...

// the method continues...

// at the end I have

CGContextEndPage (pdfContext);
CGPDFContextClose(pdfContext);
CGContextRelease (pdfContext);
</code></pre>

<p>Xcode is complaining about the last line. If I include it, it says <strong>Incorrect decrement of the reference count of an object that is not owned at this point by the caller</strong></p>

<p>If I remove the line it says: <strong>potential leak of an object allocated on line X</strong> where X is the line the context was created...</p>

<p>If I release the context on the createPDFContext method, it crashes!</p>

<p>here you have the createPDF method</p>

<pre><code>-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path
{
    CGContextRef myOutContext = NULL;
    CFURLRef url;
    CGDataConsumerRef dataConsumer;

    url = CFURLCreateWithFileSystemPath (NULL,  path, kCFURLPOSIXPathStyle, false);

    if (url != NULL)
    {
        dataConsumer = CGDataConsumerCreateWithURL (url);
        if (dataConsumer != NULL)
        {
            myOutContext = CGPDFContextCreate (dataConsumer, &amp;inMediaBox, NULL);
            CGDataConsumerRelease (dataConsumer);
        }
        CFRelease(url);
    }

//  CGContextRelease(myOutContext);
    return myOutContext;
}
</code></pre>

<p>How to solve that?</p>
