ARC-enabled TWTweetComposeViewController memory leak

I have the following code that causes a leak even though ARC is included in this file:

TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init]; [tweetViewController setInitialText:[self facebookAndTwitterStatus]]; tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) { if(result == TWTweetComposeViewControllerResultDone) { // the user finished composing a tweet } else if(result == TWTweetComposeViewControllerResultCancelled) { // the user cancelled composing a tweet } [self dismissViewControllerAnimated:YES completion:nil]; }; [self presentViewController:tweetViewController animated:YES completion:nil]; [self hideSettingsPopover]; 

Obviously, I do not have a release, but how can I get rid of this leak?

+4
source share
1 answer

Use __block in the TwTweetViewController tweetViewController variable and set tweetViewController to zero in your completion handler.

 **__block** TweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init]; [tweetViewController setInitialText:[self facebookAndTwitterStatus]]; tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) { if(result == TWTweetComposeViewControllerResultDone) { // the user finished composing a tweet } else if(result == TWTweetComposeViewControllerResultCancelled) { // the user cancelled composing a tweet } [self dismissViewControllerAnimated:YES completion:nil]; **tweetViewController = nil;** }; 

__block copies your tweetViewController and frees up when you set it to zero. This is explained in the Transition to ARC Release Notes section. http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html

Not sure why your question was rejected.

+7
source

Source: https://habr.com/ru/post/1383225/


All Articles