简体   繁体   中英

iPhone SDK: NSMutableArray count causes EXC_BAD_ACCESS

This is really twisting my mind… I'm trying to access an NSMutableArray in an IBAction which I defined in viewDidLoad. Unfortunately I keep getting a EXC_BAD_ACCESS.

I'm new to all this so I'd really appreciate some insight in what I'm doing wrong.

Below find the corresponding code excerpts.

CounterViewController.h:

@interface CounterViewController : UIViewController{
 NSMutableArray *countHistoryArray;
}
@property(nonatomic, retain) NSMutableArray *countHistoryArray;

CounterViewController.m:

@implementation CounterViewController
@synthesize countHistoryArray;

- (void)viewDidLoad {
    [super viewDidLoad];

 //Fill array with some dummy data
 self.countHistoryArray = [[NSMutableArray alloc] init];
 NSDate *now = [[[NSDate alloc] init] autorelease];
 CurrentCount *historicCount = [[[CurrentCount alloc]
         initWithCount:[NSNumber numberWithInteger:22]
         description:@"Testcount"
         dateAndTime:now] autorelease];

 [self.countHistoryArray addObject: historicCount];

 //Do some logging - everything is working fine here!
 NSLog(@"%@", [self.countHistoryArray description]); 

}


//Later on we click on a button and want to use the array
- (IBAction)doSomeStuff {  
    //Let's look at the array again - and now it crashes with EXC_BAD_ACCESS
 NSLog(@"%@", [self.countHistoryArray description]);
}

Thanks a lot!
Manuel


EDIT Additional code as asked for by @jamapag

CurrentCount.h

#import <Foundation/Foundation.h>


@interface CurrentCount : NSObject {
    NSNumber *counterLevel;
    NSString *description;
    NSDate *dateAndTime;
}

- (id)initWithCount:(NSNumber *)newCounterLevel description:(NSString *)newDescription dateAndTime:(NSDate *)newDateAndTime;

@property(nonatomic, copy) NSNumber *counterLevel;
@property(nonatomic, copy) NSString *description;
@property(nonatomic, copy) NSDate *dateAndTime;

@end

CurrentCount.m

#import "CurrentCount.h"


@implementation CurrentCount
@synthesize counterLevel;
@synthesize description;
@synthesize dateAndTime;

- (id)initWithCount:(NSNumber *)newCounterLevel description:(NSString *)newDescription dateAndTime:(NSDate *)newDateAndTime{
    self = [super init];
    if(nil != self){
        self.counterLevel = newCounterLevel;
        self.description  = newDescription;
        self.dateAndTime  = newDateAndTime;
    }
    return self;
}


-(void) dealloc{
    self.counterLevel = nil;
    self.description  = nil;
    self.dateAndTime  = nil;
    [super dealloc];
}

@end

Are you sure that your code actually looks like this?

- (IBAction)doSomeStuff {  
    //Let's look at the array again - and now it crashes with EXC_BAD_ACCESS
    NSLog(@"%@", [self.countHistoryArray description]);
}

Your question title says "NSMutableArray count causes EXC_BAD_ACCESS" - if that line of code actually says NSLog(@"%@", [self.countHistoryArray count]); , you'll almost certainly get a crash, since NSLog will attempt to treat a primitive type (the type returned by -[NSArray count] ) as an object. In order to use -[NSArray count] in NSLog , use %u instead of %@ :

- (IBAction)doSomeStuff {  
    // This time it should work!
    NSLog(@"Array Count = %u", [self.countHistoryArray count]);
}

I know this question has already been solved and accepted but its for others who are or will face this issue.

I was facing the same issue, I tried all solutions but no solution worked for me. The project I am working on is NON-ARC.

I tried and made a simple change in property

Previously my property for NSMUTABLEARRAY was

@property (nonatomic, assign) NSMutableArray * dataArray;

I changed it to:

@property (nonatomic, retain) NSMutableArray * dataArray;

Changed it from ASSIGN to RETAIN

And it solved my problem

从以下位置删除自动释放

currentCount *historicCount = [[[CurrentCount alloc] initWithCount:[NSNumber numberWithInteger:22] description:@"Testcount" dateAndTime:now] autorelease];

It looks like you are accidentally releasing countHistoryArray somewhere. Try removing all calls to it except for those two you showed. Additionally you can try enabling zombies to debug the problem.

Oh and by the way you probably don't really want a public NSMutableArray property and if you do you probably want it to be copy, not retain. Otherwise incapsulation kinda goes down the drain.

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