Strange iPhone memory leak in xml parser

Update : I edited the code, but the problem persists ...

Hi everyone,
this is my first post here - I found this place a great source for solving many of my questions. Usually I try my best to fix things on my own, but this time I really don’t know what will go wrong, so I hope someone can help me.
I am creating an iPhone application that parses a couple of xml files using TouchXML. I have an XMLParser class that takes care of loading and parsing the results. I get memory leaks when I parse an xml file more than once with the same XMLParser instance. Here is one of the parsing fragments (only the corresponding part):

for(int counter = 0; counter < [item childCount]; counter++) {  
        CXMLNode *child = [item childAtIndex:counter];
        if([[child name] isEqualToString:@"PRODUCT"]) 
        {
            NSMutableDictionary *product = [[NSMutableDictionary alloc] init];
            for(int j = 0; j < [child childCount]; j++) {
                CXMLNode *grandchild = [child childAtIndex:j];
                if([[grandchild stringValue] length] > 1) {
                    NSString *trimmedString = [[grandchild stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                    [product setObject:trimmedString forKey:[grandchild name]];
                }
            }

            // Add product to current category array
            switch (categoryId) {
                case 0:
                    [self.mobil addObject: product];
                    break;
                case 1:
                    [self.allgemein addObject: product];
                    break;
                case 2:
                    [self.besitzeIch addObject: product];
                    break;
                case 3:
                    [self.willIch addObject: product];
                    break;
                default:
                    break;
            }
            [product release];
        }

    }  

, xml, , (NSCFString/NSCFDictionary).
CXMLNode.m, :

theStringValue = [NSString stringWithUTF8String:(const char *)theXMLString];
if ( _node->type != CXMLTextKind )
   xmlFree(theXMLString);
}

return(theStringValue);  

, , , , - ?

, !

+3
3

, :

[self.mobil addObject:[product copy]];

product, NSMutableDictionary 1. mobil, , , addObject:, 2. , , , setFoo: addObject:, , autoreleased ; , , .

- , , , , mobil - , 0. [product release] for product, .

, :

[self.mobil addObject:product];
+3

, , copy, release/autorelease.

, copy , product .

+1

. , , , - , .

1) , :

@interface XMLParser : NSObject {

// ...  
NSMutableArray *mobil;  
// ...  
}   
@property(nonatomic, retain) NSMutableArray *mobil;  
@end  

, , :
  self.mobil = nil,
, , :
  [self.mobil removeAllObjects];

2) dealloc , ( mobil ):
  - (void) dealloc {
    [ ],
    self.mobil = nil,
}

, , - , - :-)

+1

Source: https://habr.com/ru/post/1710903/


All Articles