iPhone再造成一次内存泄漏

时间:2010-07-22 01:21:51

标签: iphone

aBook在.h

中定义

xml parser.h

#import <UIKit/UIKit.h>

@class XMLAppDelegate, Book,ZoneCheck;

@interface XMLParser : NSObject {

    NSMutableString *currentElementValue;

    XMLAppDelegate *appDelegate;
    Book *aBook; 
    ZoneCheck *aZonecheck;

    NSString *keyInProgress;
    NSMutableString *textInProgress;

}

- (XMLParser *) initXMLParser;

@end

xmparser.m

- (XMLParser *) initXMLParser {

    [super init];

    appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];

    return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {
//  NSLog(@"fXML        DATAT                                MCLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL");

    /////////// testing for location based ZONE
    //NSLog(@"found name");

        if ( [elementName isEqualToString:@"Location"]) {


            aZonecheck=[[ZoneCheck  alloc]init];
        //NSLog(@"found name");

    }
    //////////////////


    if ( [elementName isEqualToString:@"id"]) {
        //NSLog(@"found rootelement");
        appDelegate.books = [[NSMutableArray alloc] init];

        //[self parser:xmlParser didStartElement:@"state"  attributes:dictionary];
        //[self parser:xmlParser didStartElement:@"State"  attributes:dictionary];

        //aBook = [[Book alloc] init];  

        //Extract the attribute here.
    //  aBook.name= [[attributeDict objectForKey:@"state"] stringValue];
    //  aBook.notes=[[attributeDict objectForKey:@"notes"]stringValue];
        //[self parser:xmlParser didStartElement:@"State"  attributes:dictionary];


        return;
    }

    **else if ( [elementName isEqualToString:@"restra"] ) {
        aBook = [[Book alloc] init];
        //[abook release] cant do that???
        return;
    }**
    else if ( [elementName isEqualToString:@"totalResult"] ) {
        //aBook = [[Book alloc] init];
        //NSLog(@"found state tttttttttttt   ");
        return;
    }



    if ( [elementName isEqualToString:@"cuisines"]) {
    //  NSLog(@"found FUSSSSS");

        if ( [elementName isEqualToString:@"cuisine"] ) {

        //  NSLog(@"found CUSSSSSSSSSSSS   ");

            //elementName=[[NSMutableString alloc] init];

            keyInProgress = [elementName copy];
        //  NSLog(@"CAN YOU SEE THIS",keyInProgress);
            // This is a string we will append to as the text arrives
            textInProgress = [[[NSMutableString alloc] init]autorelease];

            return;
        }



        return;
    }

    }

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

    //NSLog(@"Processing Value: %@", currentElementValue);
    [textInProgress appendString:string];


}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    /////////// testing for locations


    if ( [elementName isEqualToString:@"Locations"]) {
        return;

    }
    if ( [elementName isEqualToString:@"Location"]) {

        [appDelegate.zone addObject:currentElementValue];

        [aZonecheck setValue:currentElementValue forKey:elementName];




        NSUserDefaults *saveZone = [NSUserDefaults standardUserDefaults];
        [saveZone setObject:currentElementValue forKey:@"keyToZone"];
        return;

    }


    //////////////









    //NSLog(@"SHKLSHLHLSHLHSLS");


    if([elementName isEqualToString:@"id"])
        return;

    //There is nothing to do if we encounter the Books element here.
    //If we encounter the Book element howevere, we want to add the book object to the array
    // and release the object.
    if([elementName isEqualToString:@"restra"]) {


        [appDelegate.books addObject:aBook];

        [aBook release];
        aBook = nil;




    }


    //if([elementName isEqualToString:@"uniqueId"]) {




    if ([elementName isEqual:keyInProgress]) {
        if ( [elementName isEqualToString:@"cuisines"]) {
            //NSLog(@"found FUSSSSS");

            if ( [elementName isEqualToString:@"cuisine"] ) {

                //[elementName setObject:currentLink forKey:@"cuisine"];

                //NSLog(@"found CUSSSSSSSSSSSS   ");
                return;
            }


            return;
        }



    }

        else 

        [aBook setValue:currentElementValue forKey:elementName];
    [aZonecheck setValue:currentElementValue forKey:elementName];

    [currentElementValue release];
    currentElementValue = nil;


}




- (void) dealloc {

    [aBook release];
    [aZonecheck release];
    [currentElementValue release];
    [super dealloc];
}

@end

我应该在didstart或didEnd中发布一本书?/

2 个答案:

答案 0 :(得分:0)

如果发布呼叫退出应用程序 - 您在调试器中看到了什么?是EXC_BAD_ACCESS吗?

如果是这样,您可能已损坏堆。以下是解决问题的几种方法:

http://www.loufranco.com/blog/files/debug-iphone-crash-EXC_BAD_ACCESS.html

答案 1 :(得分:0)

因为你在方法中分配了aBook 并且你已经用方法发布了......它应该可以正常工作。

在dealloc方法中写

  • (void)dealloc {

如果(ABOOK)     [aBook发布];

如果(aZonecheck)     [aZonecheck发布]; 如果(currentElementValue)     [currentElementValue release];     [super dealloc]; }