views:

33

answers:

1

Hello,

I'm reading PDF file, and then releasing it:

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)@"somepdf.pdf", NULL, NULL);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);    
int pagesCount = CGPDFDocumentGetNumberOfPages(pdf);
CGPDFDocumentRelease(pdf);

But the memory is not freed after release (checked that with Instruments). Why? What i'm missing in memory managment.

Thanks.

EDIT

here is my code:

- (void)loadView {
            [super loadView];
            CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)@"some.pdf", NULL, NULL);
            pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); 
            CFRelease(pdfURL);
            CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, 1);

            TiledPDFView * v = [[TiledPDFView alloc] initWithFrame:self.view.bounds andScale:1];
            [v setPage:pdfPage];

            [self.view addSubview:v];


            UIButton * but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [but setTitle:@"removeView" forState:UIControlStateNormal];
            [but addTarget:self action:@selector(tests) forControlEvents:UIControlEventTouchDown];
            but.frame = CGRectMake(0, 0, 100, 40);
            [self.view addSubview:but];

    }

    -(void) tests {
        [self.view removeFromSuperview];
        [self.view release];
        CGPDFDocumentRelease(pdf);
    }

pdf is instance variable. TiledPDFView - is uiview from ZoomingPDFViewer example. it draws CGPDFPageRef using CATiledLayer.

after i call tests method, the view is removed (becomes invisible), but the memory allocated with CGPDFDocumentCreateWithURL is not freed.

+2  A: 

What memory is not released? As you say, you duly released the CGPDFDocument, so that should have gone away.

Are you sure it's not the CFURL that's sticking around? You don't show yourself releasing that, but you Copied it, so you are obliged to. See the Memory Management Guide for Core Foundation.

You can use the ObjectAlloc instrument to determine which specific objects are remaining alive. Set the beginning and end points in the timeline to before the object was created and after it was freed, respectively, then set the instrument to show you “Objects Created & Still Living”. You can also use the Leaks instrument to show you what objects are still living that you no longer have a pointer to. Both instruments initially show a breakdown by class, into which you can drill down to instances and then events (allocations, retentions, autoreleases, releases, and deallocations).

Peter Hosey
The memory which is allocated with CGPDFDocumentCreateWithURL is not freed (~3MB is allocated and still used). I edited my code in question
Maciulis
Shouldn't you be setting the `self.view` property to `nil` rather than releasing the view you still hold onto? Also, have you verified that the page (which I assume the view retained) got released?
Peter Hosey