diff options
author | Paul Gardiner <paul.gardiner@artifex.com> | 2013-09-24 16:56:08 +0100 |
---|---|---|
committer | Paul Gardiner <paul.gardiner@artifex.com> | 2013-09-24 16:56:08 +0100 |
commit | aa8bf97eb104986fae3245bc675bd50dded01804 (patch) | |
tree | 4c2b67e31aacf9ee508b997bc02b5bb9694e0077 /platform/ios/Classes | |
parent | 32b292944f3a99edaaf40fa00265d0276f8e0346 (diff) | |
download | mupdf-aa8bf97eb104986fae3245bc675bd50dded01804.tar.xz |
iOS: use ObjC-level ref counting to control lifetime of fz_document
With the latest version if iOS, timing changes were causing crashes
during close down of a MuDocumentController. This change isolates
us from those changes.
Diffstat (limited to 'platform/ios/Classes')
-rw-r--r-- | platform/ios/Classes/MuDocRef.h | 18 | ||||
-rw-r--r-- | platform/ios/Classes/MuDocRef.m | 35 | ||||
-rw-r--r-- | platform/ios/Classes/MuDocumentController.h | 4 | ||||
-rw-r--r-- | platform/ios/Classes/MuDocumentController.m | 16 | ||||
-rw-r--r-- | platform/ios/Classes/MuLibraryController.h | 4 | ||||
-rw-r--r-- | platform/ios/Classes/MuLibraryController.m | 15 | ||||
-rw-r--r-- | platform/ios/Classes/MuPageView.h | 4 | ||||
-rw-r--r-- | platform/ios/Classes/MuPageView.m | 8 |
8 files changed, 79 insertions, 25 deletions
diff --git a/platform/ios/Classes/MuDocRef.h b/platform/ios/Classes/MuDocRef.h new file mode 100644 index 00000000..ceefcc59 --- /dev/null +++ b/platform/ios/Classes/MuDocRef.h @@ -0,0 +1,18 @@ +// +// MuDocRef.h +// MuPDF +// +// Copyright (c) 2013 Artifex Software, Inc. All rights reserved. +// + +#import <Foundation/Foundation.h> + +#include "mupdf/fitz.h" + +@interface MuDocRef : NSObject +{ +@public + fz_document *doc; +} +-(id) initWithFilename:(char *)aFilename; +@end diff --git a/platform/ios/Classes/MuDocRef.m b/platform/ios/Classes/MuDocRef.m new file mode 100644 index 00000000..60cfa99d --- /dev/null +++ b/platform/ios/Classes/MuDocRef.m @@ -0,0 +1,35 @@ +// +// MuDocRef.m +// MuPDF +// +// Copyright (c) 2013 Artifex Software, Inc. All rights reserved. +// + +#include "common.h" +#import "MuDocRef.h" + +@implementation MuDocRef + +-(id) initWithFilename:(char *)aFilename; +{ + self = [super init]; + if (self) + { + dispatch_sync(queue, ^{}); + doc = fz_open_document(ctx, aFilename); + if (!doc) + self = nil; + } + return self; +} + +-(void) dealloc +{ + __block fz_document *block_doc = doc; + dispatch_async(queue, ^{ + fz_close_document(block_doc); + }); + [super dealloc]; +} + +@end diff --git a/platform/ios/Classes/MuDocumentController.h b/platform/ios/Classes/MuDocumentController.h index f5d6115a..b15d629a 100644 --- a/platform/ios/Classes/MuDocumentController.h +++ b/platform/ios/Classes/MuDocumentController.h @@ -14,10 +14,12 @@ #include "mupdf/fitz.h" #import "MuOutlineController.h" +#import "MuDocRef.h" @interface MuDocumentController : UIViewController <UIScrollViewDelegate, UISearchBarDelegate> { fz_document *doc; + MuDocRef *docRef; NSString *key; MuOutlineController *outline; UIScrollView *canvas; @@ -34,7 +36,7 @@ int current; // currently visible page int scroll_animating; // stop view updates during scrolling animations } -- (id) initWithFilename: (NSString*)nsfilename document: (fz_document *)aDoc; +- (id) initWithFilename: (NSString*)nsfilename document: (MuDocRef *)aDoc; - (void) createPageView: (int)number; - (void) gotoPage: (int)number animated: (BOOL)animated; - (void) onShowOutline: (id)sender; diff --git a/platform/ios/Classes/MuDocumentController.m b/platform/ios/Classes/MuDocumentController.m index 89ee0da8..0b4f7d2e 100644 --- a/platform/ios/Classes/MuDocumentController.m +++ b/platform/ios/Classes/MuDocumentController.m @@ -41,14 +41,15 @@ static void flattenOutline(NSMutableArray *titles, NSMutableArray *pages, fz_out @implementation MuDocumentController -- (id) initWithFilename: (NSString*)filename document: (fz_document *)aDoc +- (id) initWithFilename: (NSString*)filename document: (MuDocRef *)aDoc { self = [super init]; if (!self) return nil; key = [filename retain]; - doc = aDoc; + docRef = [aDoc retain]; + doc = docRef->doc; dispatch_sync(queue, ^{}); @@ -154,14 +155,7 @@ static void flattenOutline(NSMutableArray *titles, NSMutableArray *pages, fz_out - (void) dealloc { - if (doc) { - fz_document *self_doc = doc; // don't auto-retain self here! - dispatch_async(queue, ^{ - printf("close document\n"); - fz_close_document(self_doc); - }); - } - + [docRef release]; docRef = nil; doc = NULL; [indicator release]; indicator = nil; [slider release]; slider = nil; [sliderWrapper release]; sliderWrapper = nil; @@ -508,7 +502,7 @@ static void flattenOutline(NSMutableArray *titles, NSMutableArray *pages, fz_out if ([view number] == number) found = 1; if (!found) { - MuPageView *view = [[MuPageView alloc] initWithFrame: CGRectMake(number * width, 0, width-GAP, height) document: doc page: number]; + MuPageView *view = [[MuPageView alloc] initWithFrame: CGRectMake(number * width, 0, width-GAP, height) document: docRef page: number]; [canvas addSubview: view]; if (showLinks) [view showLinks]; diff --git a/platform/ios/Classes/MuLibraryController.h b/platform/ios/Classes/MuLibraryController.h index 2bb7ff71..0f823d5d 100644 --- a/platform/ios/Classes/MuLibraryController.h +++ b/platform/ios/Classes/MuLibraryController.h @@ -13,11 +13,13 @@ #include "mupdf/fitz.h" +#import "MuDocRef.h" + @interface MuLibraryController : UITableViewController <UIActionSheetDelegate> { NSArray *files; NSTimer *timer; - fz_document *_doc; // temporaries for juggling password dialog + MuDocRef *doc; NSString *_filename; } - (void) openDocument: (NSString*)filename; diff --git a/platform/ios/Classes/MuLibraryController.m b/platform/ios/Classes/MuLibraryController.m index cc5a1ddd..7f21b303 100644 --- a/platform/ios/Classes/MuLibraryController.m +++ b/platform/ios/Classes/MuLibraryController.m @@ -69,6 +69,7 @@ static void showAlert(NSString *msg, NSString *filename) - (void) dealloc { + [doc release]; [files release]; [super dealloc]; } @@ -163,13 +164,14 @@ static void showAlert(NSString *msg, NSString *filename) printf("open document '%s'\n", filename); _filename = [nsfilename retain]; - _doc = fz_open_document(ctx, filename); - if (!_doc) { + [doc release]; + doc = [[MuDocRef alloc] initWithFilename:filename]; + if (!doc) { showAlert(@"Cannot open document", nsfilename); return; } - if (fz_needs_password(_doc)) + if (fz_needs_password(doc->doc)) [self askForPassword: @"'%@' needs a password:"]; else [self onPasswordOkay]; @@ -193,7 +195,7 @@ static void showAlert(NSString *msg, NSString *filename) char *password = (char*) [[[alertView textFieldAtIndex: 0] text] UTF8String]; [alertView dismissWithClickedButtonIndex: buttonIndex animated: TRUE]; if (buttonIndex == 1) { - if (fz_authenticate_password(_doc, password)) + if (fz_authenticate_password(doc->doc, password)) [self onPasswordOkay]; else [self askForPassword: @"Wrong password for '%@'. Try again:"]; @@ -204,22 +206,19 @@ static void showAlert(NSString *msg, NSString *filename) - (void) onPasswordOkay { - MuDocumentController *document = [[MuDocumentController alloc] initWithFilename: _filename document: _doc]; + MuDocumentController *document = [[MuDocumentController alloc] initWithFilename: _filename document: doc]; if (document) { [self setTitle: @"Library"]; [[self navigationController] pushViewController: document animated: YES]; [document release]; } [_filename release]; - _doc = NULL; } - (void) onPasswordCancel { [_filename release]; printf("close document (password cancel)\n"); - fz_close_document(_doc); - _doc = NULL; } @end diff --git a/platform/ios/Classes/MuPageView.h b/platform/ios/Classes/MuPageView.h index ab141ea8..58d1c8a7 100644 --- a/platform/ios/Classes/MuPageView.h +++ b/platform/ios/Classes/MuPageView.h @@ -13,10 +13,12 @@ #include "mupdf/fitz.h" +#import "MuDocRef.h" #include "MuHitView.h" @interface MuPageView : UIScrollView <UIScrollViewDelegate> { + MuDocRef *docRef; fz_document *doc; fz_page *page; int number; @@ -30,7 +32,7 @@ float tileScale; BOOL cancel; } -- (id) initWithFrame: (CGRect)frame document: (fz_document*)aDoc page: (int)aNumber; +- (id) initWithFrame: (CGRect)frame document: (MuDocRef*)aDoc page: (int)aNumber; - (void) displayImage: (UIImage*)image; - (void) resizeImage; - (void) loadPage; diff --git a/platform/ios/Classes/MuPageView.m b/platform/ios/Classes/MuPageView.m index 53fe79b9..6fa17f54 100644 --- a/platform/ios/Classes/MuPageView.m +++ b/platform/ios/Classes/MuPageView.m @@ -113,11 +113,12 @@ static UIImage *renderTile(fz_document *doc, fz_page *page, CGSize screenSize, C @implementation MuPageView -- (id) initWithFrame: (CGRect)frame document: (fz_document*)aDoc page: (int)aNumber +- (id) initWithFrame: (CGRect)frame document: (MuDocRef *)aDoc page: (int)aNumber { self = [super initWithFrame: frame]; if (self) { - doc = aDoc; + docRef = [aDoc retain]; + doc = docRef->doc; number = aNumber; cancel = NO; @@ -152,12 +153,13 @@ static UIImage *renderTile(fz_document *doc, fz_page *page, CGSize screenSize, C dispatch_async(dispatch_get_main_queue(), ^{ [block_self dealloc]; }); } else { __block fz_page *block_page = page; - __block fz_document *block_doc = doc; + __block fz_document *block_doc = docRef->doc; dispatch_async(queue, ^{ if (block_page) fz_free_page(block_doc, block_page); block_page = nil; }); + [docRef release]; [linkView release]; [hitView release]; [tileView release]; |