1

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

Ali
  • 4,205
  • 4
  • 25
  • 40
  • 1
    I'm also seeing a leak of the array I assign to excludedActivityTypes in my own code. I don't even use a completionHandler, so that's not the problem. I'm using iOS 6.0.1. – Geoff Hackworth Nov 19 '12 at 11:40
  • Humm Interesting. Have you tried to set the array to `nil` after assigning it to the `execludedActivityTypes` property? I tried it and it still leaks. – Ali Nov 19 '12 at 13:49
  • 1
    I don't have an array, I'm doing this: `activityViewController.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];`. If I assign the array to a temporary variable instead of `excludedActivityTypes` there is no leak (just as I'd expect!) Not that it should make any difference, I am not using ARC. – Geoff Hackworth Nov 19 '12 at 20:32
  • I see. In my case I'm using ARC and it's leaking weather I use a temporary array and then set it to nil or not use an array at all. The problem is the UIViewActivityController is new and there is no sample code or enough help online. – Ali Nov 20 '12 at 06:25

2 Answers2

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;
    }];
Community
  • 1
  • 1
gcamp
  • 14,622
  • 4
  • 54
  • 85
  • Sorry I'm made it confusing by using the name shareController twice. Once for the UIActivityViewController and the other inside the block for a MainViewController. I changed the name now, so inside the block the MainViewController is named someController. – Ali Nov 18 '12 at 19:13
  • Where is viewControllers coming from then? If that's an instance variable, then `self` is retained and you should __weak it the same way. – gcamp Nov 18 '12 at 19:33
  • The whole `-(IBAction)share:(id)sender` is in a `scrollViewController` where `viewControllers` is a property. And this `viewControllers` is an array of `MainViewController` instances. So `self` here is the `scrollViewController` and I don't think it's being retained inside the block cuz isn't used at all I guess. I hope I'm getting you right. Thanks. – Ali Nov 18 '12 at 19:44
  • Using `viewControllers` is exactly like `self->viewControllers`. So yeah, self is retained here. – gcamp Nov 19 '12 at 01:15
  • OK. I tried your solution to the retain cycle problem `__weak id blockShareController = shareController; [shareController setCompletionHandler:^(NSString *activityType, BOOL completed)` but the leak is still there and is pointing to `shareController.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeAssignToContact,UIActivityTypeCopyToPasteboard,UIActivityTypePostToWeibo, UIActivityTypePrint, nil];` with 75% and to `[self presentViewController:shareController animated:YES completion:nil];` with 25% So it could be the block causing the leak and not the array. – Ali Nov 19 '12 at 07:18
  • Also there is a new OS_dispacth_queue leak as shown in the updated question pic. Not just the NSArray. – Ali Nov 19 '12 at 07:21
  • I used the __weak solution and it was still leaking in the simulator. I checked on the device and there was no leak. I guess that was the problem. – Ali Nov 21 '12 at 17:20
0

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;
}];
General Failure
  • 2,421
  • 4
  • 23
  • 49
E.H
  • 1
  • 1