Eloy_007
Eloy_007

Reputation: 246

Objective-C memory leak in C methods

Hi all I get memory leaks on the leaks instrument whenever I trigger this method. Do you think anything is wrong with this??

static NSString* readStringOrDie(InputData *input, size_t length, NSStringEncoding encoding) 
{
    NSString *str = [[NSString alloc] initWithBytes: readOrDie(input,length) 
                                         length: length
                                       encoding: encoding]; // HERE THE LEAK COMES !!
    if (!str)
        [NSException raise: BERParserException format: @"Unparseable string"];

    return [str autorelease];
}    

Upvotes: 0

Views: 115

Answers (2)

Sungwook Kim
Sungwook Kim

Reputation: 354

Because of using NSException for recoverable error.

Upvotes: 0

Tommy
Tommy

Reputation: 100652

That code is fine. Most likely the actor that receives str retains it and subsequently fails to release it. Instruments knows where memory that has leaked was allocated but doesn't know where it should have been released, so it can provide some slightly unhelpful clues.

Upvotes: 3

Related Questions