NSArray * excludedActivities即使使用ARC并将其设置为nil,也会泄漏内存

时间:2012-11-18 18:00:51

标签: ios memory-leaks nsarray uiactivityviewcontroller

我正在尝试使用新的iOS6 UIActivityViewController,它工作正常,只是Memory Leaks仪器说每次我试图显示控制器时NSArray * execludedActivities都在泄漏。

请注意,我尝试使用名为excludedActivities的NSArray,然后设置shareShareController.excludedActivityTypes以便稍后将数组设置为nil(所有这些都是下面的注释代码)但是现在我直接设置属性并且仍然存在是泄漏。

- (IBAction)share:(id)sender
{

    //prepare the image
    UIImage *theImage = [self screenShot];

    //The array of activity Items
    NSArray *activityItems = [[NSArray alloc] initWithObjects:theImage, nil];


   //The acitivyViewController
   UIActivityViewController *shareController = [[UIActivityViewController alloc]   initWithActivityItems:activityItems applicationActivities:nil];

//Excluded Actvivity Types
//NSArray *excludedAcitivities = [[NSArray alloc]   initWithObjects:UIActivityTypeAssignToContact,UIActivityTypeCopyToPasteboard,UIActivityTypePostToWeibo, UIActivityTypePrint, nil];

shareController.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeAssignToContact,UIActivityTypeCopyToPasteboard,UIActivityTypePostToWeibo, UIActivityTypePrint, nil];

    //testing fixning the leak of NSArray
    //excludedAcitivities = nil;

    //set the completion handler
    [shareController setCompletionHandler:^(NSString *activityType, BOOL completed) {

        //test hiding the By MunasabaPro lable
        int shareScreen = pageControl.currentPage;
        MainViewController *someController = [viewControllers objectAtIndex:shareScreen];
        someController.byLabel.hidden = YES;
    }];

    [self presentViewController:shareController animated:YES completion:nil];
}

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为你的完成处理程序中有一个保留周期。看一下question

__weak id blockShareController = shareController;
[shareController setCompletionHandler:^(NSString *activityType, BOOL completed) {

       //test hiding the By MunasabaPro lable
        int shareScreen = pageControl.currentPage;
        blockShareController.byLabel.hidden = YES;
    }];

答案 1 :(得分:0)

试试这个,这应该可以解决问题(至少在我的情况下):

NSArray *activityItems = [NSArray arrayWithObjects:textToShare, imageToShare, nil];

__block UIActivityViewController *activityVC =[[UIActivityViewController alloc] 
initWithActivityItems:activityItems applicationActivities:nil];

activityVC.excludedActivityTypes = @[
    UIActivityTypePrint,
    UIActivityTypeAssignToContact
];

[self presentViewController:activityVC animated:YES completion:^{
    activityVC.excludedActivityTypes = nil;
    activityVC = nil;
}];