MayurCM
MayurCM

Reputation: 701

Should have a non-void return value to indicate whether or not an error occurred

Hi every1 below is my code in which I'm getting memory leaks. I'm really new to handling memory leak so please be kind to me even if its a simple bug.

-(void) parseActivityData:(NSMutableData*) data parseError:(NSError **)error
{
    NSXMLParser* parser = [[NSXMLParser alloc] initWithData:data];

    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];

    [parser parse];
    NSError *parseError = [parser parserError];
    if (parseError && error) {
        *error = parseError;
    }
    [*error retain];

    [parser release];   
}

for this code I'm getting this memory leak issues one is "Method accepting NSError should have a non-void return value to indicate whether or not an error occurred" and the other is DeDereference of null pointer(loaded from variable 'error'). Thanks

Upvotes: 2

Views: 4588

Answers (2)

Michael Dautermann
Michael Dautermann

Reputation: 89509

Try something like this:

-(BOOL) parseActivityData:(NSMutableData*) data parseError:(NSError **)error
{
    NSXMLParser* parser = [[NSXMLParser alloc] initWithData:data];

    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];

    BOOL success = [parser parse];
    // only assign error if parser FAILED
    if(success == NO)
    {
        // error should be autoreleased... no need to retain
        *error = [parser parserError];

        // if the above line doesn't work (if error also releases
        // when you release the parser object), then take out the above
        // line of code and uncomment this line below:
        // 
        // *error = [[parser parserError copy];
        //
        // the above line makes a retained copy of the error, which
        // you must release in the caller.  
    }
    return success;
}

Upvotes: 0

alediaferia
alediaferia

Reputation: 2617

Simply do what's requested. Return a BOOL instead of void that is set to NO when you actually use the error param. Also do not [*error retain];.

Upvotes: 6

Related Questions