0

通过访问 Quartz 2D 编程指南和 SO 上的一些帖子,我拼凑了一些代码来读取 PDF 文件的内容。我认为它在 PDF 扫描仪部分失败了。当我释放扫描仪时,应用程序因以下错误而崩溃:

avi(2282,0x3de7bb88) malloc: *** error for object 0x693f7c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

我正在通过电子邮件附件打开 PDF 文件。我知道它至少可以识别文件,因为我可以获得页数。有人可以看看我下面的代码,看看我的错误发生在哪里吗?谢谢。

- (void) readPDFFile:(NSURL *)PDFURL {

    //make a pdf document and point it to the url of our pdf file
    CGPDFDocumentRef pdfDocument;
    pdfDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)(PDFURL));

    //release the url
    CFRelease((__bridge CFTypeRef)(PDFURL));


    if (pdfDocument) {

        //get page count
        int pageCount = CGPDFDocumentGetNumberOfPages (pdfDocument);

        //
        if (pageCount > 0) {

            //set up operator table
            CGPDFOperatorTableRef pdfOpTable;
            pdfOpTable = CGPDFOperatorTableCreate();

            //call backs for Op Table
            CGPDFOperatorTableSetCallback (pdfOpTable, "MP", &op_MP);
            CGPDFOperatorTableSetCallback (pdfOpTable, "DP", &op_DP);
            CGPDFOperatorTableSetCallback (pdfOpTable, "BMC", &op_BMC);
            CGPDFOperatorTableSetCallback (pdfOpTable, "BDC", &op_BDC);
            CGPDFOperatorTableSetCallback (pdfOpTable, "EMC", &op_EMC);

            for(int i=0; i<pageCount; i++) {

                //set up
                CGPDFPageRef thisPage;
                CGPDFScannerRef pdfScanner;
                CGPDFContentStreamRef thisContentStream;

                //get the page
                thisPage = CGPDFDocumentGetPage (pdfDocument, i + 1 );

                //get the page content stream
                thisContentStream = CGPDFContentStreamCreateWithPage (thisPage);

                //create a pdf scanner using our previously created table and callbacks
                pdfScanner = CGPDFScannerCreate (thisContentStream, pdfOpTable, NULL);

                //scan the pdf file
                CGPDFScannerScan (pdfScanner); //-->call backs happen here

                //release everything
                CGPDFPageRelease (thisPage);
                /*CGPDFScannerRelease (pdfScanner);
                CGPDFContentStreamRelease (thisContentStream);*/


            }
        }
    }

    //release the pdf document
    CGPDFDocumentRelease(pdfDocument);


}

static void op_MP (CGPDFScannerRef s, void *info) {

    const char *name;

    if (!CGPDFScannerPopName(s, &name))
        return;

    NSLog(@"MP /%s\n", name);
}

static void op_DP (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    NSLog(@"DP /%s\n", name);
}

static void op_BMC (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    NSLog(@"BMC /%s\n", name);
}

static void op_BDC (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    NSLog(@"BDC /%s\n", name);
}

static void op_EMC (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    NSLog(@"EMC /%s\n", name);
}

编辑:PDFURL 来自此过程:用户选择使用应用程序打开附加到电子邮件的 PDF 文件。在 AppDelegate.m 中:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
   /* NSLog(@"Open URL:\t%@\n"
          "From source:\t%@\n"
          "With annotation:%@",
          url, sourceApplication, annotation);*/

    NSMutableDictionary* userData = [[NSMutableDictionary alloc] init];

    [userData setObject:@"Read PDF" forKey:@"Action"];
    [userData setObject:url forKey:@"File Path"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"theMessenger" object:self userInfo: userData];

    //NSString *filepath = [url path];
    //...
    return YES;
}

在 viewcontroller.m 中

- (void) receiveNotification:(NSNotification* ) notification {
...
} else if ([strAction isEqualToString:@"Read PDF"]) {

            NSURL* documentURL = [[notification userInfo] objectForKey:@"File Path"];
            [self readPDFFile:documentURL];

        }
...

编辑:如果我不发布此页面应用程序仍然崩溃:

libobjc.A.dylib`objc_release: 0x3be89f20: cmp r0, #0 0x3be89f22: it eq 0x3be89f24: bxeq lr 0x3be89f26: ldr r1, [r0] 0x3be89f28: movs r2, #2 0x3be89f2a: #160, [r0] ldr r1, [rf2] : bfi r1, r2, #0, #2 0x3be89f30: ldrb r1, [r1] 0x3be89f32: tst.w r1, #2 0x3be89f36: bne 0x3be89f3e ; objc_release + 30 0x3be89f38: movs r1, #0 0x3be89f3a: bw 0x3be981c0 ; -[NSObject release] 0x3be89f3e: movw r1, #19326 0x3be89f42: movt r1, #507 0x3be89f46: add r1, pc 0x3be89f48: ldr r1, [r1] 0x3be89f4a: bw 0x3be875a0 ; objc_msgSend 0x3be89f4e: nop

4

1 回答 1

1

不要发布此页面..您通过 GET 调用而不是副本或创建方法获得它,这意味着您不拥有它

--

不要发布 PDURL .. 你从外面得到它但你不拥有它

--

释放 operatorTable .. 你创建它

不要将它重复用于多个扫描仪

于 2013-05-23T19:42:19.190 回答