NSXMLParser leaking
Here i am trying to parse the xml data coming from server side of my application. But this code is showing a leak on instruments.
TPatient is NSObject with following variable: FirstName,LastName,BirthDate,Phone,Email,Password,Chart,Referredby,RecordConsult,Doctor,Assistant,Notes,PatientStatus,PatientUUID.
-(NSMutableArray *)FromXML:(NSString *)xmlstring
{
curr_patient = nil;
self.curr_patient_array = nil;
last_string = nil;
NSData *data = [xmlstring dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
[xmlParser parse];
[xmlParser setDelegate:nil];
[xmlParser release];
if(curr_patient!=nil) {
[curr_patient release];
curr_patient=nil;
}
if(last_string!=nil)
{
[last_string 开发者_如何学Pythonrelease];
last_string=nil;
}
while ( PARSE_STATE == 0 )
;
if ( PARSE_STATE == 1 ) {
if ( self.curr_patient_array!= nil ) {
[self.curr_patient_array release ];
self.curr_patient_array = nil;
return nil;
}
}
if ( self.curr_patient_array == nil )
return nil;
return [self.curr_patient_array autorelease];
}
-(void)parser:(NSXMLParser*)parser didStartElement:(NSString *)
elementName namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ( [elementName isEqualToString:@"Patients" ] == YES )
{
if(self.curr_patient_array !=nil)
{
[self.curr_patient_array release];
self.curr_patient_array=nil;
}
self.curr_patient_array = [NSMutableArray new ];
}
else if ( [elementName isEqualToString:@"Patient" ] == YES )
{
if ( curr_patient != nil )
[curr_patient release ];
curr_patient = [TPatient new ];
}
}
-(void)parser:(NSXMLParser*)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString*)
namespaceURI qualifiedName:(NSString *)qName
{
NSString *curString;
if(last_string==nil)
{
curString=@"";
}
else
{
curString=last_string;
[curString retain];
}
if ( [elementName isEqualToString:@"Patientid" ] == YES )
{
NSInteger it = [ curString intValue];
curr_patient.PatientId = it;
[last_string release ];
last_string=nil;
}
else if ( [elementName isEqualToString:@"Patient" ] == YES )
{
[self.curr_patient_array addObject:curr_patient ];
[curr_patient release];
curr_patient = nil;
}
[curString release];
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
last_string = [NSString stringWithFormat:@"%@"
,string];
[last_string retain];
}
Can anyone help me to find the memory leak in following code.
Change:
self.curr_patient_array = [NSMutableArray new];
To:
NSMutableArray *newPatientArray = [NSMutableArray new];
self.curr_patient_array = newPatientArray;
[newPatientArray release];
Also curr_patient = [TPatient new];
could end up being a problem. Every time you call this line you need to be sure to release it somewhere. You might want to switch it to a synthesized variable like the NSMutableArray above and allocate it the same way.
And read this: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447
Note that [NSObject new]
is the same as [[NSObject alloc] init]
.
精彩评论