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) {
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 ... 