NSXMLParser 多次调用 - 访问错误

发布于 2024-10-22 05:10:42 字数 4795 浏览 4 评论 0原文

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

header:

#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-file:

#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

hello i want to parse html an with this information another html file...
after 1-5 call the program crashes...

header:

#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-file:

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小霸王臭丫头 2024-10-29 05:10:42

这很简单。您将两次发布 ParseHTML NSXMLPArse。

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

  2. -(void)dealloc.

仅当您赢得了对象的所有权时才应释放该对象。通过保留副本等。但是您只分配了一次,因此应该只释放一次。但你发布了两个版本。

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

问候,
杰克逊·桑尼·罗德里格斯

that is simple. You are releasing ParseHTML NSXMLPArsetwice.

  1. -(void) imagePickerController: (UIImagePickerController*) reader
    didFinishPickingMediaWithInfo: (NSDictionary*) info
    in the lastline

  2. -(void)dealloc.

A object should be release only if you have earned the ownership. by retain copy etc. But you have allocated it only once so should release only once. But you did two releases .

You are also releasing NSXMLParser object parseHTML2 thrice. As per your code at any stage parseHTML2 will be released at least twice which is retained only once. ParseHTML1 objects case have been mentioned above

Regards,
Jackson Sunny Rodrigues

生活了然无味 2024-10-29 05:10:42

打开 NSZombieEnabled。显然你正在释放一些你不应该释放的东西。当您执行此操作时,它将准确地显示发生错误访问的位置,并且您可以追溯到释放对象的位置。查看本教程:

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

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

Turn on NSZombieEnabled. You are obviously releasing something you shouldn't be. When you do this, it will show you exactly where the bad access is occurring and you can trace back to where you are releasing the object. Check out this tutorial:

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

Best to learn how to fix it and what's wrong :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文