tags:

views:

184

answers:

2

Hi guys,

I am getting leak at [pool release];

My code here is:

#pragma mark UISearchBarDelegate delegate methods


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

NSAutoreleasePool * pool = [[NSAutoreleasePool 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.

+1  A: 

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?

Vladimir
yes I tries as up to ur suggestion but now it showing leak at the line:artistName= [aSearchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
monish
When I taken off the thread the search is very slow actually I am getting the data from the parser.
monish
I mean why you need to exit from thread using [NSThread exit] function? Did you try just remove [NSThread exit] call?
Vladimir
I tried it by removing the thread totally....actually I called the method performSelector using thread.now I an not using any thread to call it and i directly calling the method using self by passing searchbar as an argument.
monish
You can run this method using performSelectorInBackground: - it will run your method in separate thread but you will not need to mess with NSThread objects directly.
Vladimir
Ok thanks alot Ill do tat.
monish
A: 

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];
tedge
thanks very much Vladimir and tedge its working fine I totally removed the thread from my code.
monish