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!
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…