views:

59

answers:

1

Hi,

I'm trying to clean my app from leaks with Leak instrument. It shows me leaks on xml parser (TBXML).

Here is a class I'm going to create upon the parsing:

@interface GraphPoint : NSObject {
    NSString* x;
    NSString* y;
}


@property (nonatomic, copy) NSString* x;
@property (nonatomic, copy) NSString* y;

@end

@implementation GraphPoint

@synthesize x, y;

... some calculations

- (void) dealloc
{
    [x release];
    [y release];
    [super dealloc];
}

@end

In the parser:

... // When found according element:

        NSString    *str;
        GraphPoint  *aPoint = [[GraphPoint alloc] init];

        TBXMLElement *item = [TBXML childElementNamed:kX_Item parentElement:pntItem];
        str = [TBXML textForElement:item];  
        aPoint.x = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];     

        item = [TBXML childElementNamed:kY_Item parentElement:pntItem];
        str = [TBXML textForElement:item];  
        aPoint.y = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


        [points addObject:aPoint];
        [aPoint release];

Leaks instrument shows leak in TBXML's textForElement function, which provides autoreleased string:

+ (NSString*) textForElement:(TBXMLElement*)aXMLElement {
    if (nil == aXMLElement->text) return @"";
    return [NSString stringWithCString:&aXMLElement->text[0] encoding:NSUTF8StringEncoding];
}

Since we're talking sometimes about hundreds or even thousands of points, these leaks become something huge. I cannot understand why autoreleased string produce leaks?

Any thoughts?

Thanks

A: 

There are no retain/release problems in the posted code. The only allocation in textForElement: is NSString's stringWithCString:encoding:, which I doubt to leak.

The problem is elsewhere and cannot be answered with the given information. Are you sure that you are reading Instruments results correctly? What does static analysis say about the code?

I don't know about the TBXML library, but the line containing nil == aXMLElement->text makes it look a bit fishy. It's not an error but a question of style: aXMLElement->text seems to be a C string while nil is used for objc objects.

Nikolai Ruhe
Thanks, you were right, the leak was in other place - the array, that was holding points arrays wasn't deallocated
Nava Carmon