NSArray* excludedActivities Leaks memory even when using ARC and setting it to nil

坚强是说给别人听的谎言 提交于 2019-12-24 15:27:09

问题


I'm trying to use the new iOS6 UIActivityViewController and it works fine except that the Memory Leaks instrument says that the NSArray *execludedActivities is leaking every time I try to show the controller.

Note that I tried to use an NSArray called excludedActivities and then set the shareShareController.excludedActivityTypes to be able to set the array to nil later (all that is commented code below) but now I'm setting the property directly and still there is a leak.

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


回答1:


I think you have a retain cycle in your completion handler. Take a look at that 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;
    }];



回答2:


Try this and this should resolve the problem (at least for my case):

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


来源:https://stackoverflow.com/questions/13442910/nsarray-excludedactivities-leaks-memory-even-when-using-arc-and-setting-it-to-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!