NSXMLParser多次调用 - BAD ACCESS

时间:2011-03-17 15:14:24

标签: iphone delegates nsxmlparser exc-bad-access

你好我想解析html和这个信息另一个html文件... 1-5之后,程序崩溃......

头:

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController <ZBarReaderDelegate, NSXMLParserDelegate>{

    UIImageView *resultImage;
    UITextView *resultText;
    NSString *product_link;
    NSXMLParser *parseHTML;
    NSXMLParser *parseHTML2;
    NSMutableArray *myMutableArray;
    id <NSXMLParserDelegate> testkollege, asdf;
}
@property (nonatomic, retain) IBOutlet UIImageView *resultImage;
@property (nonatomic, retain) IBOutlet UITextView *resultText;
@property (nonatomic, assign) IBOutlet NSString *product_link;
@property (nonatomic, assign) NSXMLParser *parseHTML;
@property (nonatomic, assign) NSXMLParser *parseHTML2;
@property (nonatomic, retain) NSMutableArray *myMutableArray;
@property (nonatomic, assign) id <NSXMLParserDelegate> testkollege;
@property (nonatomic, assign) id <NSXMLParserDelegate> asdf;
- (IBAction) scanButtonTapped;

@end

m文件:

#import "FirstViewController.h"
#import "/System/Library/Frameworks/Foundation.framework/Headers/NSDebug.h"

@implementation FirstViewController
@synthesize resultImage, resultText;
@synthesize product_link;
@synthesize parseHTML, parseHTML2;

@synthesize myMutableArray;
@synthesize testkollege, asdf;

bool link_is_here = false;
bool allergy_is_here = false; 
bool parse_one_ok = true;

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{

    // ADD: get the decode results
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        // EXAMPLE: just grab the first barcode
        break;

    // EXAMPLE: do something useful with the barcode data
    resultText.text = symbol.data;    
    // EXAMPLE: do something useful with the barcode image
    resultImage.image =
    [info objectForKey: UIImagePickerControllerOriginalImage];

    // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader dismissModalViewControllerAnimated: YES];

    parseHTML = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:[@"http://url.com/suche/?q=" stringByAppendingString:symbol.data]] ];  
    NSLog(@"parser 1 start");
    [parseHTML setDelegate:self];
    [parseHTML parse]; 
    NSLog(@"parser 1 ready");
    [parseHTML release];

}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    for(NSString *key in [attributeDict allKeys]) {
        if ([[attributeDict valueForKey:key] isEqualToString:@"search-result"]) {
            link_is_here = true;        
        }
        if ([key isEqualToString:@"href"] && link_is_here) {
            product_link = [attributeDict valueForKey:key];
            [parser abortParsing];
            parseHTML2 = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:[@"http://url.com" stringByAppendingString:product_link]]];
            [parseHTML2 setDelegate:self];
            parse_one_ok = true;
            link_is_here = false;
            [parseHTML2 parse];

        }
        if ([key isEqualToString:@"id"] && [[attributeDict valueForKey:key] isEqualToString:@"nutrition-allergy"]) {
            allergy_is_here = true;
        }
        if ([key isEqualToString:@"title"] && allergy_is_here) {      
            NSLog(@"keys:  %@",[attributeDict valueForKey:key]);  
        }
        if ([key isEqualToString:@"id"] && [[attributeDict valueForKey:key] isEqualToString:@"another string"]) {
            allergy_is_here = false;
            parse_one_ok = true;
            NSLog(@"Parser off");
            [parser abortParsing];
        } 
    }
}
-(void) parserDidEndDocument:(NSXMLParser *)parser{
    if (parse_one_ok) {

        [parseHTML2 release];
        parse_one_ok = false;
    }
}

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

}  

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [parseHTML release];
    [parseHTML2 release];
    self.product_link = nil;
    self.resultImage = nil;
    self.resultText = nil;
    [super dealloc];
}

@end

2 个答案:

答案 0 :(得分:1)

这很简单。您正在发布ParseHTML NSXMLPArsetwice。

  1. - (void)imagePickerController:(UIImagePickerController *)阅读器 didFinishPickingMediaWithInfo:(NSDictionary *)info 在最后一行

  2. - (无效)的dealloc

  3. 只有获得所有权后才能释放对象。保留副本等但你只分配了一次所以应该只发布一次。但你做了两次发布。

    您还要发布NSXMLParser对象parseHTML2三次。根据您在任何阶段的代码,parseHTML2将至少发布两次,仅保留一次。 ParseHTML1对象案例已在上面提到

    此致 Jackson Sunny Rodrigues

答案 1 :(得分:0)

启用NSZombieEnabled。你显然发布了一些你不应该发布的东西。执行此操作时,它将向您显示错误访问的确切位置,并且您可以追溯到释放对象的位置。看看这个教程:

http://www.codza.com/how-to-debug-exc_bad_access-on-iphone

最好学习如何修复它以及出了什么问题:)