Note that there are some explanatory texts on larger screens.

plurals
  1. POUIGraphicsGetImageFromCurrentImageContext memory leak with previews
    text
    copied!<p>I'm trying to create previews images of pages in a PDF but I have some problems with the release of memory.</p> <p>I wrote a simple test algorithm that cycles on the problem, <strong>the app crashes near the 40th iteration</strong>:</p> <pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myPdf.pdf"]; CFURLRef url = CFURLCreateWithFileSystemPath( NULL, (CFStringRef)pdfPath, kCFURLPOSIXPathStyle, NO ); CGPDFDocumentRef myPdf = CGPDFDocumentCreateWithURL( url ); CFRelease (url); CGPDFPageRef page = CGPDFDocumentGetPage( myPdf, 1 ); int i=0; while(i &lt; 1000){ UIGraphicsBeginImageContext(CGSizeMake(768,1024)); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); CGContextFillRect(context,CGRectMake(0, 0, 768, 1024)); CGContextSaveGState(context); CGContextTranslateCTM(context, 0.0, 1024); CGContextScaleCTM(context, 1.0, -1.0); CGContextDrawPDFPage(context, page); CGContextRestoreGState(context); // -------------------------- // The problem is here (without this line the application doesn't crash) UIImageView *backgroundImageView1 = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()]; // -------------------------- UIGraphicsEndImageContext(); [backgroundImageView1 release]; NSLog(@"Loop: %d", i++); } CGPDFDocumentRelease(myPdf); </code></pre> <p>The above-mentioned line seems to generate a memory leak, however, <em>instruments</em> doesn't show memory problems;</p> <p>Can I escape from this kind of mistake?someone can explain me in which way? Are there other ways to show previews of a pdf?</p> <p><strong>UPDATE</strong></p> <p>I think the problem isn't the release of <code>UIImage</code> created by the method <code>UIGraphicsGetImageFromCurrentImageContext()</code> but the release of <code>UIImageView</code> created with this autorelease image.</p> <p>I have divided the line of code in three steps:</p> <pre><code>UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext(); UIImageView *myImageView = [[UIImageView alloc] init]; [myImageView setImage: myImage]; // Memory Leak </code></pre> <p>The first and second lines doesn't create memory leaks so I think that the method UIGraphicsGetImageFromCurrentImageContext is not the problem.</p> <p>I also tried as follows but the problem persists:</p> <pre><code>UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage]; </code></pre> <p>I think there is a memory leak in the release of a UIImageView that contains a UIImage with the autorelease property.</p> <p>I tried to write my object UIImageView inheriting a UIView as explained in this <a href="https://stackoverflow.com/questions/4644912/iphone-uiimageview-setimage-leaks">thread</a>.</p> <p>This solution works but isn't very elegant, it's a workaround, I would prefer to use the object UIImageView solving the memory problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload