user768373
user768373

Reputation: 19

Memory Leak In line of code

My app is working fine, but when I run instrument for checking for leaks, it shows me a leak at this line of code, in purple with a 100.0% mark:

xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

Here's the method containing this line:

-(NSString*) languageSelectedStringForKey:(NSString*) key
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"zh" ofType:@"lproj"];

    if(selectedLanguage==French)
    {
        FinalString = [[NSString alloc] initWithFormat:@"http://www.xyz.com/api_com.php?page_id=%d",IDValue];
        url = [[NSURL alloc] initWithString:FinalString];
    }
    else if(selectedLanguage==German)
    {
        FinalString = [[NSString alloc] initWithFormat:@"http://www.x.com/api_com.php?page_id=%d",IDValue];
        url = [[NSURL alloc] initWithString:FinalString];
    }
    else if(selectedLanguage==Nepali)
    {
        FinalString = [[NSString alloc] initWithFormat:@"http://www.xy.com/api_com.php?page_id=%d",IDValue];
        url = [[NSURL alloc] initWithString:FinalString];
    }
    xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [url release];

    //Initialize the delegate.
    parser = [[NewsParser alloc] initXMLParser];
    //Set delegate
    [xmlParser setDelegate:parser];

    //Start parsing the XML file.
    BOOL success = [xmlParser parse];
    if(success)
        NSLog(@"No Errors");
    else
        NSLog(@"Error Error Error!!!");

    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
    return str;
}

Here's my ViewDidLoad method from which languageSelectedStringForKey is called.

- (void)viewDidLoad
{
    // Do any additional setup after loading the view from its nib.
    appDelegate = (ProgAppDelegate *)[[UIApplication sharedApplication] delegate];
    IDValue = 1;
    textLabel.text=[self languageSelectedStringForKey:@"Welcome to Advance Localization"];
    [super viewDidLoad];
}

What is causing this leak, and how can I fix it?

this is dealloc method:-

- (void)dealloc
{

    [xmlParser release];
    [parser release];
    [nibLoadedCell release];

    [super dealloc];
}

Upvotes: 2

Views: 298

Answers (3)

deanWombourne
deanWombourne

Reputation: 38475

You never release FinalString (at least not in the code you posted)

this is held in the URL, which is held by the parser :)


Also, have you considered what would happen if this function is called twice?

Each time you say

xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

you would leak the previous xmlParser ;)

If you are allocating to an instance variable, you have to remember to release the previous object i.e.

[xmlParser release];
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

Upvotes: 0

Max MacLeod
Max MacLeod

Reputation: 26652

You need to make NewsParser parser an instance variable and release it in the dealloc. Above, you init it, but you don't release it. Of course, you can't because it's a delegate of xmlParser. So, to make sure the object is retained, then properly released, it must be an ivar.

Upvotes: 0

Matthew Gillingham
Matthew Gillingham

Reputation: 3429

Do you ever call

[xmlParser release];

?

If not, you should release it when you no longer need it. Perhaps in the dealloc method of the same class in which that line appears.

Upvotes: 3

Related Questions