我把苹果的示例代码“推特”并修改它,以便它不使用ARC(download my project here to test the problem yourself)。实际上只需要一个“自动释放”声明:
// 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];
如果您发送推文或点击“tweetViewController”中的取消,您可能会期望取消分配tweetViewController。而是对象保留在内存中,包括附加的图像(如果您在推文中附加了一个)。因此,如果用户试图发出另一条推文,应用程序的内存会因为tweetViewController泄漏而变大。
正如您所注意到的那样,块中没有提到tweetViewController,因此它不会自动保留..
我使用了工具并证明1“[[TWTweetComposeViewController alloc] init]”会导致内存泄漏。
我试图证明它的另一件事是“NSLog(”%@“,tweetViewController);”控制器被解雇后的一些运行周期。通常程序应该崩溃,因为tweetViewController不会指向TWTweetComposeViewController实例。相反,它正确打印了前一个实例,证明了内存泄漏。
我为避免这种情况而找到的唯一方法是违反内存管理规则,如下:
[tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
// Dismiss the tweet composition view controller using animated YES!!
[self dismissModalViewControllerAnimated:YES];
[tweetViewController release];
}];
如果你在没有动画的情况下解雇它,就会发生内存泄漏......你有没有注意到这个问题?难道我做错了什么?亲自试试并评论一下......
答案 0 :(得分:2)
原来这是一个已知的错误,它目前正由苹果调查!我希望它在iOS 5.1中得到修复
答案 1 :(得分:0)
由于您已经自动释放它,因此您不应该在完成块中release
它。尝试在autorelease
release
来电并[self presentModalViewController:tweetViewController animated:YES];