简体   繁体   中英

NSMutableArray “EXC_BAD_ACCESS”

I've been debugging my program for hours and receive the error EXC_BAD_ACCESS every time I run it.
I followed the steps in this tutorial but with no results.

I will try to post some code and my findings, hoping that someone will help me solve my issue.

-(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];
}

It seems that the NSMutableArray *listOfItems is causing the problem.
If I add a retain to its allocation in the grabData function no more errors will occur.
If I remove the [listOfItems release]; from the dealloc function no more errors will occur.
But the most interesting is that if I remove the last line of the grabData function which is [itl release]; no more errors will occur.

I am stuck and cannot find a solution. I would be very grateful if someone can help me solve this issue.

EDIT: as requested this is the.h of the 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

and here is the function that uses the objects in 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)];
}

Best guess is that the properties of itListCell is not retaining or copying and that you are over releasing one of the values assigned to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM