【问题标题】:TWTweetComposeViewController memory leak without ARC没有 ARC 的 TWTweetComposeViewController 内存泄漏
【发布时间】: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


    【解决方案1】:

    原来这是一个已知的错误,目前正在由苹果调查!我希望它在 iOS 5.1 中得到修复

    【讨论】:

    • 这有什么更新吗?我认为这仍然可能是一个问题,但它也可能是我的代码中的其他内容。
    • 它仍然存在于 iOS 5.1 中
    【解决方案2】:

    您不必在完成块中release 它,因为您已经在自动释放它。尝试在[self presentModalViewController:tweetViewController animated:YES]; 之后删除autorelease 调用和release

    【讨论】:

    • 我知道我不应该在块内释放它,这就是为什么我说这个“解决方案”违反了内存管理规则。我已经尝试过按照您所说的发布它,没有任何区别。看看我附上的xcode项目。
    • 嗯...看起来像一个错误。保留它的东西由于某种原因没有释放它。
    猜你喜欢
    • 1970-01-01
    • 2012-04-03
    • 2012-09-14
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2013-08-19
    • 2014-04-03
    相关资源
    最近更新 更多