Note that there are some explanatory texts on larger screens.

plurals
  1. POTWTweetComposeViewController memory leak without ARC
    primarykey
    data
    text
    <p>I took apple's sample code "tweeting" and modified it so that it doesn't use ARC (<a href="http://dl.dropbox.com/u/1785075/testTwitter.zip" rel="nofollow noreferrer">download my project here to test the problem yourself</a>). Actually only one "autorelease" statement was needed:</p> <pre><code>// 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]; </code></pre> <p>If you send the tweet or click cancel in "tweetViewController" you would expect that the tweetViewController is deallocated. Instead the object remains in memory including the attached image (if you have attached one in the tweet) . So, if the user tries to make another tweet the app's memory grows bigger because tweetViewController leaked.</p> <p>As you notice, tweetViewController isn't mentioned inside the block so it isn't automatically retained.. </p> <p>I used instruments and proved <a href="http://dl.dropbox.com/u/1785075/testTwitter.zip" rel="nofollow noreferrer">1</a> that "[[TWTweetComposeViewController alloc] init]" causes a memory leak.</p> <p>Another thing I tried in order to prove it, was to " NSLog("%@",tweetViewController); " a few runcycles after the controller has been dismissed. Normally the program should have crashed because tweetViewController would not be pointing to a TWTweetComposeViewController instance. Instead it printed correctly the previous instance, proving the memory leak.</p> <p>The only way I have found in order to avoid this is by violating the memory management rules and is the following:</p> <pre><code>[tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) { // Dismiss the tweet composition view controller using animated YES!! [self dismissModalViewControllerAnimated:YES]; [tweetViewController release]; }]; </code></pre> <p>If you dismiss it without animation, a memory leak occurs... Have you noticed the problem? Am I doing something wrong? Try it yourself and comment please... <img src="https://i.stack.imgur.com/vjpLM.png" alt="instruments showing the &quot;leak&quot;"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload