NSArray的* excludedActivities使用ARC并将其设置为nil时出现内存泄漏甚

2019-10-17 09:32发布

我试图使用新的iOS6的UIActivityViewController,它工作正常,除了内存泄漏仪说,NSArray的* execludedActivities泄漏每次我试图表明控制器时间。

请注意,我试图用一个NSArray称为excludedActivities,然后设置shareShareController.excludedActivityTypes能够设置数组为零后(所有被注释掉下面的代码),但现在我直接设置属性,仍然有泄漏。

- (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];
}

Answer 1:

我认为你必须在你完成处理一个保留周期。 看看那个问题 。

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

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


Answer 2:

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

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;
}];


文章来源: NSArray* excludedActivities Leaks memory even when using ARC and setting it to nil