NSMutableArray“EXC_BAD_ACCESS”

时间:2011-07-06 10:19:15

标签: iphone objective-c nsmutablearray exc-bad-access

我一直在调试我的程序几个小时,每次运行时都会收到错误EXC_BAD_ACCESS 我按照tutorial中的步骤操作,但没有结果。

我会尝试发布一些代码和我的发现,希望有人能帮助我解决我的问题。

-(void)grabData{
    listOfItems=[[NSMutableArray alloc] init];

    NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"list.xml"];
    NSData *XMLData   = [NSData dataWithContentsOfFile:XMLPath];
    CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithData:XMLData options:0 error:nil] autorelease];

    NSArray *buttons=[rssParser nodesForXPath:@"//item" error:nil];
    for (CXMLElement *node in buttons) {
        int counter;
        if([[[node attributeForName:@"type"] stringValue] isEqualToString:@"photobutton"]){
            itListCell *itl=[[itListCell alloc] initWithNibName:@"itListCell" bundle:nil];
            itl.rows=0;
            itl.iw=0.0;
            itl.ih=0.0;
            itl.uiid=@"";
            itl.text=@"";
            itl.action=@"";
            itl.aUrl=@"";
            itl.iUrl=@"";
            for(counter = 0; counter < [node childCount]; counter++) {
                if([[[node childAtIndex:counter] name] isEqualToString:@"text"]){
                    NSString * value = [[[node childAtIndex:counter] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                    if ([value length] != 0){
                        itl.text=[[node childAtIndex:counter] stringValue];
                    }
                }
                else{
                    if([[[node childAtIndex:counter] name] isEqualToString:@"uiid"]){
                        itl.uiid=[[node childAtIndex:counter] stringValue];
                    }
                    else{
                        if([[[node childAtIndex:counter] name] isEqualToString:@"rows"]){
                            itl.rows=[[[node childAtIndex:counter] stringValue] intValue];
                        }
                        else{
                            if([[[node childAtIndex:counter] name] isEqualToString:@"icon"]){
                                itl.iUrl=[[node childAtIndex:counter] stringValue];
                                CXMLElement *n=(CXMLElement*)[node childAtIndex:counter];
                                NSString *resString=[[n attributeForName:@"resize"] stringValue];
                                NSArray *resArray= [resString componentsSeparatedByString: @","];
                                itl.iw=[[resArray objectAtIndex:0] floatValue];
                                itl.ih=[[resArray objectAtIndex:1] floatValue];
                            }
                            else{
                                if([[[node childAtIndex:counter] name] isEqualToString:@"action"]){
                                    itl.action=[[((CXMLElement*)[node childAtIndex:counter]) attributeForName:@"name"] stringValue];
                                    NSArray *getaUrl=[node elementsForName:@"action"];
                                    for(CXMLElement *n in getaUrl){
                                        for(int c = 0; c < [n childCount]; c++) {
                                            if([[[n childAtIndex:c] name] isEqualToString:@"url"]){
                                                itl.aUrl=[[n childAtIndex:c] stringValue];
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            [listOfItems addObject:itl];
            [itl release];
        }
    }
}




- (void)dealloc {
    [listOfItems release];
    [scroll release];
    [super dealloc];
}

NSMutableArray *listOfItems似乎导致问题 如果我在grabData函数中为其分配添加保留,则不会再出现错误 如果我从[listOfItems release];函数中删除dealloc,则不会再出现错误 但最有趣的是,如果我删除grabData函数的最后一行[itl release];,则不会再出现错误。

我被困住了,无法找到解决方案。如果有人能帮助我解决这个问题,我将非常感激。

编辑: 根据要求,这是itListCell的

//
//  itListCell.h
//  TemplatesTest
//
//  Created by foobyte on 6/30/11.
//  Copyright 2011 FOO. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Constants.h"
#import "imageDelegate.h"


@interface itListCell : UIViewController {
    UIImageView *i;
    UILabel *l;

    BOOL imageDidLoad;
    CGFloat width;
    CGFloat maxCalculatedHeight;
    CGSize strSize;

    //properties
    int rows;
    CGFloat iw;
    CGFloat ih;
    NSString *uiid;
    NSString *text;
    NSString *action;
    NSString *aUrl;
    NSString *iUrl;

}

@property (nonatomic, retain) UIImageView *i;
@property (nonatomic, retain) UILabel *l;
@property (nonatomic, retain) NSString *uiid;
@property (nonatomic, retain) NSString *text;
@property (nonatomic, retain) NSString *action;
@property (nonatomic, retain) NSString *aUrl;
@property (nonatomic, retain) NSString *iUrl;
@property (nonatomic) int rows;
@property (nonatomic) CGFloat iw;
@property (nonatomic) CGFloat ih;

-(void)setwidth:(CGFloat)w;
-(CGFloat)getMaxHeight;
-(UIImage *)downloadIcon:(NSString *)s;

@end

这是使用listOfItems中的对象的函数:

-(void) setupPage{
    [scroll setCanCancelContentTouches:NO];
    scroll.indicatorStyle=UIScrollViewIndicatorStyleWhite;
    scroll.clipsToBounds=YES;
    scroll.scrollEnabled=YES;
    scroll.pagingEnabled=NO;
    [scroll setBackgroundColor:[UIColor blackColor]];

    CGFloat y=0.0;
    CGFloat cy=0.0;

    int count=[listOfItems count];
    for(int i=0;i<count;i++){
        [((itListCell *)[listOfItems objectAtIndex:i]) setwidth:self.view.frame.size.width];
        CGFloat h=[((itListCell *)[listOfItems objectAtIndex:i]) getMaxHeight];
        ((itListCell *)[listOfItems objectAtIndex:i]).view.frame=CGRectMake(lITX, y, self.view.frame.size.width-lITM, h);
        [scroll addSubview:((itListCell *)[listOfItems objectAtIndex:i]).view];
        y+=h;
        if(i >= 2)
            cy+=h;
    }
    [scroll setContentSize:CGSizeMake(scroll.frame.size.width, cy+[scroll bounds].size.height)];
}

1 个答案:

答案 0 :(得分:1)

最好的猜测是itListCell的属性没有保留或复制,而且你过度释放分配给它的一个值。