【发布时间】:2012-02-25 15:08:17
【问题描述】:
我采用了苹果的示例代码“tweeting”并对其进行了修改,使其不使用 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];
}];
如果你在没有动画的情况下关闭它,就会发生内存泄漏......你注意到这个问题了吗?难道我做错了什么?请自己尝试并发表评论...
【问题讨论】:
标签: ios cocoa-touch memory memory-leaks twitter