Xcode 4.2.1:NSThread 导致内存泄漏,使用 ARC

发布于 2024-12-27 14:54:48 字数 2210 浏览 4 评论 0原文

我在 Xcode 中进行的一个学校项目即将结束,但现在我遇到了一个小但非常烦人的问题:内存泄漏。泄漏已追溯到以下代码行:

@autoreleasepool {
    [NSThread detachNewThreadSelector:@selector(updateThread) toTarget:self withObject:nil];
}

当我将其注释掉时,泄漏就消失了。显然,自动释放池中出了问题:我对这些仍然有点陌生(尤其是在使用 ARC 时),但是像 这个让我清楚地知道使用@autoreleasepool应该足够了。

由于某种原因,我的代码并非如此。我想我在这里遗漏了一些东西:如果有人可以就问题可能是什么提出一些想法,那么我们将不胜感激。只要告诉我是否必须发布更多代码,这不会成为问题:只是为了问题的可读性,我尝试将其限制为主要问题。

提前致谢!

编辑:

感谢您的第一反应!然而,问题仍然存在......我将发布更多代码来澄清一些事情。线程在 viewDidLoad 中启动:

/*
 Everything mentioned here will be done after loading.
 */
- (void)viewDidLoad
{
    // Do standard setup
    [super viewDidLoad];

    // Do any additional setup before loading the view from its nib.
    self.title = @"Blog Manager";

    // Activate edit mode
    [tbvBlogList setEditing:YES animated:YES];
    tbvBlogList.allowsSelectionDuringEditing = YES;

    [NSThread detachNewThreadSelector:@selector(updateThread) toTarget:self withObject:nil];

    UIImage *btnImage = [UIImage imageNamed:@"iPhone_General_Button_Add_Blog.png"];
    UIButton *viewBtnAddBlog = [UIButton buttonWithType:UIButtonTypeCustom];
    [viewBtnAddBlog setImage:btnImage forState:UIControlStateNormal];
    viewBtnAddBlog.frame = CGRectMake(0, 0, 80, 36);
    [viewBtnAddBlog addTarget:self action:@selector(addBlogByButton:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *btnAddBlog = [[UIBarButtonItem alloc] initWithCustomView:viewBtnAddBlog];
    btnAddBlog.tintColor = [UIColor clearColor];
    self.navigationItem.rightBarButtonItem = btnAddBlog;
}

然后,用于线程的其他函数:

/*
 Thread to update the progress bar with.
 */
- (void)updateThread
{
    @autoreleasepool {
        while(YES){
            [self performSelectorOnMainThread:@selector(updateProgressBar) withObject:nil waitUntilDone:false];
            [NSThread sleepForTimeInterval:0.1f];
        }
    }
}

/*
 Updates the progress bar.
 */
- (void)updateProgressBar
{
    pvProgress.progress = dProgress;
}

如果有什么值得一提的:我正在使用 Xcode 4.2.1。再次感谢您的支持!

I'm nearing the end of a school project with programming in Xcode, but right now I'm having a small yet extremely annoying issue: a memory leak. The leak has been traced down to the following line of code:

@autoreleasepool {
    [NSThread detachNewThreadSelector:@selector(updateThread) toTarget:self withObject:nil];
}

When I comment this out, the leak is gone. Apparently something goes wrong in the autoreleasepool: I'm still a bit new on these (especially when using ARC), but threads like this one made it clear to me that using @autoreleasepool should be sufficient.

For some reason, this is not the case for my code. I guess I'm missing something here: if someone could give some ideas on what the issue could be, then that would be highly appreciated. Just tell me if I have to post more code, that won't be a problem: it's just for the readability of the question that I try to limit it to the main issue.

Thanks in advance!

EDIT:

Thank you for the first responses! The issue still persists however... I will post a bit more code to clear things up a bit. The thread is started in viewDidLoad:

/*
 Everything mentioned here will be done after loading.
 */
- (void)viewDidLoad
{
    // Do standard setup
    [super viewDidLoad];

    // Do any additional setup before loading the view from its nib.
    self.title = @"Blog Manager";

    // Activate edit mode
    [tbvBlogList setEditing:YES animated:YES];
    tbvBlogList.allowsSelectionDuringEditing = YES;

    [NSThread detachNewThreadSelector:@selector(updateThread) toTarget:self withObject:nil];

    UIImage *btnImage = [UIImage imageNamed:@"iPhone_General_Button_Add_Blog.png"];
    UIButton *viewBtnAddBlog = [UIButton buttonWithType:UIButtonTypeCustom];
    [viewBtnAddBlog setImage:btnImage forState:UIControlStateNormal];
    viewBtnAddBlog.frame = CGRectMake(0, 0, 80, 36);
    [viewBtnAddBlog addTarget:self action:@selector(addBlogByButton:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *btnAddBlog = [[UIBarButtonItem alloc] initWithCustomView:viewBtnAddBlog];
    btnAddBlog.tintColor = [UIColor clearColor];
    self.navigationItem.rightBarButtonItem = btnAddBlog;
}

Then, the other functions that are used for the threading:

/*
 Thread to update the progress bar with.
 */
- (void)updateThread
{
    @autoreleasepool {
        while(YES){
            [self performSelectorOnMainThread:@selector(updateProgressBar) withObject:nil waitUntilDone:false];
            [NSThread sleepForTimeInterval:0.1f];
        }
    }
}

/*
 Updates the progress bar.
 */
- (void)updateProgressBar
{
    pvProgress.progress = dProgress;
}

If it is anything worth mentioning: I'm using Xcode 4.2.1. Thanks again for the support!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

今天小雨转甜 2025-01-03 14:54:48

现在我只想拿石头砸自己。

我刚刚意识到“while”循环永远不会停止。当然,这意味着线程将继续运行,因此在应用程序完成之前内存不会被释放。

通过简单地添加一个在线程应该退出时设置为“NO”的布尔值,问题就得到了解决。各位:非常感谢你们帮我看这个问题。有时最大的问题有最小的解决方案......

Right now I just want to hit myself with a rock.

I just realized the "while"-loop never stops. Of course this means the thread will keep running, therefore the memory won't ever be released until the app finishes.

By simply adding a boolean that is set to "NO" when the thread should quit, the issue was solved. Everyone: thanks you very much for looking at this problem for me. Sometimes the biggest problems have the smallest solutions...

触ぅ动初心 2025-01-03 14:54:48

@autoreleasepool 块位于线程代码中(在本例中为 updateThread),而不是围绕线程的创建。

The @autoreleasepool block goes in your thread code (updateThread in this case), not around the creation of the thread.

灼痛 2025-01-03 14:54:48

您没有在分离选择器的方法内创建自动释放池。每个线程选择器都需要自己的池。这样做:

- (void) updateThread
{
    @autoreleasepool {
        // former code here
    }
}

You're not creating an autorelease pool inside the detached selector's method. Every thread selector needs its own pool. Do like this:

- (void) updateThread
{
    @autoreleasepool {
        // former code here
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文