我正在开发一个应用程序(iPad、Xcode 4.6、iOS 6.x),它有一个模式窗口和一个在主线程上更新的 UIProgressView,一旦 UIProgressView 监视的任务完成,就会显示两个内存泄漏。如果我从代码中删除 UIProgressView 和线程调用,则不会出现泄漏。我已经使用手动创建的 UIProgressView 和 StoryBoard 中的一个进行了尝试。程序使用 ARC。
两个泄漏是 WebCore/WebThreadCurrentContext [Malloc 16 bytes] 和 UIKit/GetContextStack [Malloc 64 bytes]。
@interface KICreateImportFileViewController ()
...
@property (strong, nonatomic) UIProgressView *convertedRecordsProgressView;
@end
@implementation KICreateImportFileViewController
- (void)viewDidLoad
{
[super viewDidLoad];
...
// config the UIProgressView
CGRect pFrame = CGRectMake(20, 100, 500, 9);
self.convertedRecordsProgressView = [[UIProgressView alloc] initWithFrame:pFrame];
self.convertedRecordsProgressView.progressViewStyle = UIProgressViewStyleDefault;
[self.view addSubview:self.convertedRecordsProgressView];
}
- (void)viewDidAppear:(BOOL)animated
{
// listen for a NSNotification with info regarding process from class KITestParser
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateProgressViewWhenNotified:)
name:@"KITestParser:Progress Updated"
object:self.originalFile.testParser];
// Begin the background process that will update the UIProgressView
[self performSelectorInBackground:@selector(createImportFileInBackground:) withObject:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super viewWillDisappear:animated];
}
#pragma mark - Notifications
-(void)updateProgressViewWhenNotified:(NSNotification *)notification
{
NSNumber* progress = (NSNumber *)[notification.userInfo valueForKey:@"progressRatio"];
[self updateProgressViewOnMainThread:progress];
}
#pragma mark - Private class methods
- (void)createImportFileInBackground:(id)obj
{
// Background processes aren't automatically protected by ARC unless you wrap the function in the auto-release pool.
@autoreleasepool {
[self.originalFile createImportFile];
}
}
- (void)updateProgressViewOnMainThread:(NSNumber *)progress
{
[self performSelectorOnMainThread:@selector(updateProgressView:) withObject:progress waitUntilDone:NO];
}
- (void)updateProgressView:(NSNumber *)progress
{
[self.convertedRecordsProgressView setProgress:[progress floatValue] animated:YES];
}
@end
问题 1:从 createImportFileInBackground: 调用的所有方法是否都需要包装在 @autoreleasepool{} 中。如果是这样,这是否包括将通知发送回此类的另一个类中的方法?
问题 2:我是否遗漏了导致泄漏发生的东西?
任何帮助/建议将不胜感激。提前致谢!蒂姆