【发布时间】:2012-03-15 22:10:07
【问题描述】:
我一直在使用来自 Raphael Cruzeiro 的 PDF Annotator 的代码,并发现了一些内存泄漏(ARC 已关闭,将保持关闭以支持旧设备)。在修补了大部分之后,我只剩下最后一对了,他们让我难过。因此,在一个名为PDFDocument 的类中,他具有CGPDFPageRef、CGPDFDocument 和自定义注释类@synthesize 的属性。我不得不在他的 dealloc 方法中添加 release 并消除一些悬空指针,除了一个小问题外,这很好用:在大约 3 个完整的保留释放周期后,它在他的注释对象的 @synthesize 行崩溃......我已经由于在@synthesize 期间发送了一个释放的对象,所以从未见过 SIGABRT,所以自然不知道如何修复它。如果我删除了 dealloc 中的发布代码,它会泄漏,但如果我把它留在里面,它就会崩溃。这是 PDFDocument 类的代码:
//.h
#import <Foundation/Foundation.h>
@class Annotation;
@interface PDFDocument : NSObject {
Annotation *_annotation;
}
- (id)initWithDocument:(NSString *)documentPath;
- (NSInteger) pageCount;
- (void) loadPage:(NSInteger)number;
- (BOOL)save;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *hash;
@property (readwrite, nonatomic, assign) CGPDFDocumentRef document;
@property (readwrite, nonatomic, assign) CGPDFPageRef page;
@property (nonatomic, retain) NSString *version;
@property (nonatomic, assign) BOOL dirty;
@property (nonatomic, retain) Annotation *annotation;
@end
//.m
#import "PDFDocument.h"
#import "Annotation.h"
#import "HashExtensions.h"
#import "DocumentDeserializer.h"
#import "DocumentSerializer.h"
@implementation PDFDocument
@synthesize document;
@synthesize page;
@synthesize annotation = _annotation; //after 3rd cycle, it crashes here.
@synthesize name;
@synthesize hash;
@synthesize dirty;
@synthesize version;
- (id)initWithDocument:(NSString *)documentPath
{
if((self = [super init]) != NULL) {
self.name = [documentPath lastPathComponent];
if ([self.name isEqualToString:@"Musette.pdf"] || [self.name isEqualToString:@"Minore.pdf"] || [self.name isEqualToString:@"Cantata.pdf"] || [self.name isEqualToString:@"Finalé.pdf"])
{
CFURLRef ref = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)self.name, NULL, NULL);
self.document = CGPDFDocumentCreateWithURL(ref);
self.page = CGPDFDocumentGetPage(document, 1);
self.version = @"1.0";
DocumentDeserializer *deserializer = [[[DocumentDeserializer alloc] init] autorelease];
self.annotation = [deserializer readAnnotation:[[(NSURL*)ref absoluteString] stringByDeletingPathExtension]];
CFRelease(ref);
}
else {
CFURLRef pdfURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:documentPath];
self.document = CGPDFDocumentCreateWithURL(pdfURL);
self.page = CGPDFDocumentGetPage(document, 1);
self.version = @"1.0";
DocumentDeserializer *deserializer = [[[DocumentDeserializer alloc] init] autorelease];
self.annotation = [deserializer readAnnotation:[[(NSURL*)pdfURL absoluteString] stringByDeletingPathExtension]];
CFRelease(pdfURL);
CGPDFPageRelease(self.page);
}
}
return self;
}
- (NSInteger)pageCount
{
return CGPDFDocumentGetNumberOfPages(self.document);
}
- (void)loadPage:(NSInteger)number
{
self.page = CGPDFDocumentGetPage(document, number);
}
- (BOOL)save
{
DocumentSerializer *serializer = [[[DocumentSerializer alloc] init] autorelease];
[serializer serialize:self];
self.dirty = NO;
return !self.dirty;
}
- (void)dealloc
{
CGPDFDocumentRelease(self.document);
if (self.annotation != nil && _annotation != nil) {
[_annotation release];
self.annotation = nil;
} //my attempt to prevent the object from being over-released
self.document = nil;
self.name = nil;
[super dealloc];
}
@end
然后我通过 Instruments 运行它来查找僵尸对象,果然,Instruments 发现了一个释放的对象,它在完全相同的@synthesize 行发送了一条消息!
有谁知道发生了什么以及如何解决它?
【问题讨论】:
-
只有初代iPhone不兼容ARC,你为什么不用呢?
-
只是我自己的喜好......再加上 ARC 重构工具现在让我很恼火的事实。唉...我现在就转换。
-
你说它在“合成期间”崩溃了。实际上,它在
- (Annotation*)annotation或- (void)setAnnotation:(Annotation*)合成方法中崩溃了。可能是 ivar 已经发布的 setter。
标签: objective-c ios memory-management core-graphics dealloc