开发者

one more memory leak for iPhone

aBook is defined in .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

where should i release aBook in didstart or didEnd??/


If a release call quits the app -- what do you see in the debugger? Is it EXC_BAD_ACCESS?

If so, you probably corrupted the heap. Here are a few approaches to solving it:

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


Since u allocated the aBook in method and u have released it in method ... It should work fine .

In the dealloc method write

  • (void) dealloc {

if(aBook) [aBook release];

if(aZonecheck) [aZonecheck release]; if(currentElementValue) [currentElementValue release]; [super dealloc]; }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜