 |
 |
|
 |
06-20-2009, 07:14 AM
|
#1 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 2
|
iPhone memory leak in xml parser
Hi everyone,
I am building an iPhone app that parses a couple of xml files using TouchXML. I have a class XMLParser, which takes care of downloading and parsing the results. I am getting memory leaks when I parse an xml file more than once with the same instance of XMLParser.
Here is one of the parsing methods:
Code:
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;
}
//DebugLog(@"ADD allgemein %@",product);
[product release];
}
}
The first time, I parse the xml no leak shows up in instruments, the next time I do so, I got a lot of leaks (NSCFString / NSCFDictionary).
Instruments points me to this line in CXMLNode.m, when I dig into a leaked object:
Code:
theStringValue = [NSString stringWithUTF8String:(const char *)theXMLString];
if ( _node->type != CXMLTextKind )
xmlFree(theXMLString);
}
return(theStringValue);
I really spent a long time and tried multiple approaches to fix this, but to no avail so far, maybe I am missing something essential?
Any help is highly appreciated, thank you!
Last edited by krille; 06-20-2009 at 07:26 AM.
|
|
|
06-20-2009, 11:50 AM
|
#2 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 2
|
I fixed the problem myself. It is nothing wrong with the TouchXML Code - it was kind of stupid, but maybe someone might come across the same thing, so I am going to post it here.
1) I had a mutable array set up as instance variables like this:
Code:
@interface XMLParser : NSObject {
// ...
NSMutableArray *mobil;
// ...
}
@property(nonatomic, retain) NSMutableArray *mobil;
@end
Everytime I wanted to reset and store new data inside the array I did:
Which did not what I wanted to do, so this is the better approach:
Code:
[self.mobil removeAllObjects];
2) The dealloc method has to be like this to fix the leaks (because mobil is defined as a property):
Code:
-(void)dealloc {
[mobil release];
self.mobil = nil;
}
Whew, that has been a lot of work to find out - hope it saves someone else some time :-)
|
|
|
11-06-2009, 11:32 AM
|
#3 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 9
|
Help
Quote:
Originally Posted by krille
I fixed the problem myself. It is nothing wrong with the TouchXML Code - it was kind of stupid, but maybe someone might come across the same thing, so I am going to post it here.
1) I had a mutable array set up as instance variables like this:
Code:
@interface XMLParser : NSObject {
// ...
NSMutableArray *mobil;
// ...
}
@property(nonatomic, retain) NSMutableArray *mobil;
@end
Everytime I wanted to reset and store new data inside the array I did:
Which did not what I wanted to do, so this is the better approach:
Code:
[self.mobil removeAllObjects];
2) The dealloc method has to be like this to fix the leaks (because mobil is defined as a property):
Code:
-(void)dealloc {
[mobil release];
self.mobil = nil;
}
Whew, that has been a lot of work to find out - hope it saves someone else some time :-)
|
Hi Friend,
I am going through a similar problem. I am also using a custom parser to parse incoming data. But when I check it through Leaks tool, I find the most of the objects I use are leaking. Pleas could you help me?.
|
|
|
11-06-2009, 05:33 PM
|
#4 (permalink)
|
|
Registered Member
Join Date: Oct 2009
Posts: 52
|
[quote=krille;95037]
Your dealloc method should include a call to super dealloc. Leaving that off, and it's pretty easy to do, can cause an object to stick around though the retain count for the object is actually zero.
Example dealloc
Code:
-(void)dealloc {
[mobil release];
self.mobil = nil;
[super dealloc];
}
Last edited by Iphoneer; 11-06-2009 at 08:16 PM.
Reason: Erase bad info!!!
|
|
|
11-09-2009, 07:32 AM
|
#5 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 9
|
[quote=Iphoneer;140330]
Quote:
Originally Posted by krille
Your dealloc method should include a call to super dealloc. Leaving that off, and it's pretty easy to do, can cause an object to stick around though the retain count for the object is actually zero.
Example dealloc
Code:
-(void)dealloc {
[mobil release];
self.mobil = nil;
[super dealloc];
}
|
Hi Friend, I am using a custom XML parser. But even after releasing it it leaks. Please help me. I have provided the code below:
+(id) requestServiceWithInMemoryContext: (NSManagedObjectContext *)managedObjContext  NSString *)methodName : (NSDictionary *)params : (RequestTypes)requestType
{
NSURL *url=[self getURL: methodName : params];
HWXMLParser *xmlParser = [[HWXMLParser alloc] initWithContext:managedObjContext RequestType:requestType];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:xmlParser];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
if ( [parser parserError] ) {
NSLog(@"Parse error %@", [parser parserError]);
if(![HWUtility isNetWorkAvailable])
{
[xmlParser release];
[parser release];
return nil;
}
}
[parser release];
[xmlParser saveManagedObjectContext];
returnValue= [xmlParser getParsedResults];
xmlParser=nil;
[xmlParser release];
return returnValue;
}
The returnValue is
id returnValue;
Thank you..
Krishnan.
|
|
|
11-09-2009, 09:24 AM
|
#6 (permalink)
|
|
Emphasizing Fundamentals
Join Date: Jul 2009
Location: Loveland, OH
Age: 34
Posts: 1,691
|
Code:
HWXMLParser *xmlParser = [[HWXMLParser alloc] initWithContentsOfURL:url];
...
xmlParser=nil;
[xmlParser release];
Release before nil.
|
|
|
11-13-2009, 01:41 PM
|
#7 (permalink)
|
|
Registered Member
Join Date: Oct 2009
Posts: 52
|
Quote:
Originally Posted by BrianSlick
Code:
HWXMLParser *xmlParser = [[HWXMLParser alloc] initWithContentsOfURL:url];
...
xmlParser=nil;
[xmlParser release];
Release before nil.
|
I disagree.
Why send a release message to a pointer that you know points to nil? I know doing so is harmless, but if you are going to use both statements, wouldn't the reverse order be more logical.
|
|
|
11-13-2009, 01:43 PM
|
#8 (permalink)
|
|
Emphasizing Fundamentals
Join Date: Jul 2009
Location: Loveland, OH
Age: 34
Posts: 1,691
|
Quote:
Originally Posted by Iphoneer
I disagree.
Why send a release message to a pointer that you know points to nil? I know doing so is harmless, but if you are going to use both statements, wouldn't the reverse order be more logical.
|
I think you misread something.
|
|
|
11-13-2009, 05:03 PM
|
#9 (permalink)
|
|
Registered Member
Join Date: Oct 2009
Posts: 52
|
Quote:
Originally Posted by BrianSlick
I think you misread something.
|
Yes, I seemed to have erred...
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 500 |
| 44 members and 456 guests |
| bbc z, BostonMerlin, cjamerlan, CoolApps, coolman, CunningCat, dany88, dbarrett, dda, designomatt, embedded, Gamer211, ggalante, gonk, imsatasia, IphoneSdk, jharrah, Kalimba, LemonMeringue, Link, loobian, markbuchanan, Maximilian, MiniRobinho, montage, mquetel, naomipunkclan, Noise, pashik, Pring, saul102, Snappy, soulless, StefanL, Tambourin, the1nz4ne, treazer, TunaNugget, Tuszy, victorsk, wassupdoc, williamlegate, x2on, ZunePod |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 21,508
Threads: 35,792
Posts: 156,797
Top Poster: smasher (2,449)
|
| Welcome to our newest member, JoelSelwood7 |
|