NSXMLParser泄漏

时间:2011-05-19 04:09:51

标签: iphone ipad memory-management memory-leaks nsxmlparser

这里我试图解析来自应用程序服务器端的xml数据。但是这段代码显示了仪器泄漏。

TPatient是NSObject,带有以下变量:         名字,姓氏,出生日期,电话,电子邮件,密码,图表,Referredby,RecordConsult,医生,助手,注意事项,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 release];
        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];
}   

任何人都可以帮我找到以下代码中的内存泄漏。

1 个答案:

答案 0 :(得分:0)

变化:

self.curr_patient_array = [NSMutableArray new];

要:

NSMutableArray *newPatientArray = [NSMutableArray new];
self.curr_patient_array = newPatientArray;
[newPatientArray release];

curr_patient = [TPatient new];也可能最终成为一个问题。每次调用此行时,您都需要确保将其释放到某处。您可能希望将其切换为合成变量,如上面的NSMutableArray,并以相同的方式分配它。

请阅读:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447

请注意,[NSObject new][[NSObject alloc] init]相同。