TWTweetComposeViewController memory leak without ARC

I took the tweeting example code and changed it so that it would not use ARC ( download my project here to test the problem yourself ). In fact, only one expression "autorelease" is required:

// Set up the built-in twitter composition view controller. tweetViewController = [[[TWTweetComposeViewController alloc] init]autorelease]; // Set the initial tweet text. See the framework for additional properties that can be set. [tweetViewController setInitialText:@"Hello. This is a tweet."]; // Create the completion handler block. [tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) { NSString *output; switch (result) { case TWTweetComposeViewControllerResultCancelled: // The cancel button was tapped. output = @"Tweet cancelled."; break; case TWTweetComposeViewControllerResultDone: // The tweet was sent. output = @"Tweet done."; break; default: break; } //[self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO]; // Dismiss the tweet composition view controller. [self dismissModalViewControllerAnimated:NO]; }]; // Present the tweet composition view controller modally. [self presentModalViewController:tweetViewController animated:YES]; 

If you submit a tweet or click Cancel in the tweetViewController, you expect tweetViewController to be released. Instead, the object remains in memory, including the attached image (if you added it to a tweet). So, if the user tries to make another tweet, the application’s memory grows more because it missed tweetViewController.

As you noticed, tweetViewController is not mentioned inside the block, so it is not saved automatically.

I used tools and proved 1 that "[[TWTweetComposeViewController alloc] init]" causes a memory leak.

Another thing I tried to prove is "NSLog ("% @ ", tweetViewController)," several cycles after the controller was fired. Normally, the program should be broken because tweetViewController will not point to an instance of TWTweetComposeViewController. Instead, he correctly printed the previous instance, proving a memory leak.

The only way I found to avoid this is to violate memory management rules and the following:

 [tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) { // Dismiss the tweet composition view controller using animated YES!! [self dismissModalViewControllerAnimated:YES]; [tweetViewController release]; }]; 

If you reject it without animation, a memory leak will occur ... Have you noticed a problem? Am I doing something wrong? Try it yourself and please comment ... instruments showing the "leak"

+4
source share
2 answers

It turns out that this is a known mistake, and is currently being investigated by an apple! I hope it will be fixed in iOS 5.1

+2
source

You do not need to release it in the completion block, since you are already auto-implementing it. Try removing the call to autorelease and release after [self presentModalViewController:tweetViewController animated:YES];

0
source

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


All Articles