开发者

Memory leak at Autorelease pool in Iphone sdk

I am getting leak at [pool release];

My code here is:

#pragma mark UISearchBarDelegate delegate methods


- (void)performSearch:(UISearchBar *)aSearchBar
{

NSAutoreleasePool * pool = [[NSAut开发者_运维问答oreleasePool alloc] init];

artistName= [aSearchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([artistName length] > 0) 
{

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    LyricsAppDelegate* appDelegate =  (LyricsAppDelegate*) [ [UIApplication sharedApplication] delegate];
    artistsList=[appDelegate doSearch:artistName ];
    [theTableView reloadData];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;


    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;


    [aSearchBar resignFirstResponder];
}
else
{
    [aSearchBar resignFirstResponder];
}
[NSThread exit];
[pool release]; 

}

- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar
{
@try {
    [NSThread detachNewThreadSelector:@selector(performSearch:) toTarget:self withObject:aSearchBar];
    [aSearchBar resignFirstResponder];
}
@catch (NSException * e) {
    NSLog(@"\n caught an exception");
}
@finally {
}
}

Here I am getting leak at [pool release]; in performSearch method.

How can I solve this.

Anyone's help will be much appreciated.

Thank you, Monish.


Try to release pool before you exit current thread?

...
[pool release]; 
[NSThread exit];

Edit: From NSThread -exit reference:

Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution.

Do you really need to call this function BTW?


In addition to Vladimir's answer pointing out the autorelease pool leak, both artistName & artistList are missing a release message before you set them to a new value (otherwise the old object is leaked), and a retain message afterwards (so the new object sticks around when the autorelease pool is drained).

[artistsList release];
artistsList = [[appDelegate doSearch:artistName] retain];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜