Sudhanshu
Sudhanshu

Reputation: 3960

Memory Leak related

i am working on a fishing app these days and i am getting a memory leak problem

 -(void)requestFinished:(ASIFormDataRequest *) request {

        if(hud != nil){
            [hud show:NO];
            [hud release];
            hud = nil;
        }
        isLoading = NO;
        self.responseText = [request responseString];
        [self parseXml];       //I am getting leak here  
        if ( [self.responseText hasPrefix:@"<result>"]) {
            UIAlertView *info = [[[UIAlertView alloc] initWithTitle:@" " message:@"Limited Internet access, please find a stronger signal in the area" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]autorelease];
            [info show];
        }

if (!isRefreshButtonClicked) {      
            [UIAccelerometer sharedAccelerometer].delegate = self;
            [NSThread detachNewThreadSelector:@selector(parseXml) toTarget:self withObject:nil];
        } }

This is my function...

-(void) parseXml
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    _fishes = [[fishes parseXml:self.responseText] retain];

    [self performSelectorOnMainThread:@selector(parseXmlDone) withObject:nil waitUntilDone:YES];
      [pool release];

Here _fishes is an array which is getting a value from a array return type function.....and here is that function...

+(NSMutableArray *)parseXml:(NSString *)xmlString {
    //xmlString = [xmlString stringByReplacingOccurrencesOfString:@"&" withString:@""];
    const char *cString = [xmlString UTF8String];
    NSMutableArray *fishes = [NSMutableArray array];
    NSData *xmlData = [NSData dataWithBytes:cString length:strlen(cString)];
    NSError *error;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:&error];
    if (doc == nil) { return nil; }
    //parseXml
    NSArray *_fishes = [doc.rootElement elementsForName:@"fishery"];
    for (GDataXMLElement *_fish in _fishes) {
        NSMutableDictionary *fish = [NSMutableDictionary dictionary];
        NSArray *ids = [_fish elementsForName:@"id"];
        if ([ids count]>0) {
            GDataXMLElement *firstId = (GDataXMLElement *)[ids objectAtIndex:0];
            [fish setValue:firstId.stringValue forKey:@"id"];
        } else continue;
        NSArray *names = [_fish elementsForName:@"name"];
        if ([names count]>0) {
            GDataXMLElement *firstName = (GDataXMLElement *)[names objectAtIndex:0];
            [fish setValue:firstName.stringValue forKey:@"name"];...........


    ........
else continue;
        NSArray *distances = [_fish elementsForName:@"distance"];
        if ([distances count]>0) {
            GDataXMLElement *distance = (GDataXMLElement *)[distances objectAtIndex:0];
            [fish setValue:distance.stringValue forKey:@"distance"];
        }else continue;
        [fishes addObject:fish];
    }
    [doc release];
    return fishes;
}
@end

I hope u guys will understand my problem...thanx

Upvotes: 0

Views: 294

Answers (1)

user557219
user557219

Reputation:

In -parseXml,

_fishes = [[fishes parseXml:self.responseText] retain];

will leak any previous object _fishes was pointing to in case -parseXml is sent more than once. You could use a retain property instead of an instance variable, or a setter method that releases the previous object, or release the previous object before assigning a new (retained) object to _fishes.

Upvotes: 1

Related Questions