Search code examples
iosmemory-leaksnsarrayuiactivityviewcontroller

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


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

enter image description here


Solution

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