iPhone SDK:NSMutableArray计数导致EXC_BAD_ACCESS

时间:2010-08-02 08:21:43

标签: iphone nsmutablearray exc-bad-access

这真的扭曲了我的想法......我正在尝试访问我在viewDidLoad中定义的IBAction中的NSMutableArray。不幸的是,我一直在获得EXC_BAD_ACCESS。

我对这一切都不熟悉,所以我真的很感激我对自己做错的一些见解。

下面找到相应的代码摘录。

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

非常感谢!
曼努埃尔


编辑 @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

4 个答案:

答案 0 :(得分:10)

您确定您的代码实际上是这样的吗?

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

您的问题标题显示“NSMutableArray count会导致EXC_BAD_ACCESS” - 如果该行代码实际上显示NSLog(@"%@", [self.countHistoryArray count]);,您几乎肯定会遇到崩溃,因为NSLog会尝试处理原始类型( -[NSArray count])返回的类型作为对象。要在-[NSArray count]中使用NSLog,请使用%u代替%@

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

答案 1 :(得分:0)

中删除 autorelease

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

答案 2 :(得分:0)

看起来你不小心在某处释放了countHistoryArray。尝试删除除此之外的所有调用。此外,您可以尝试启用zombies来调试问题。

哦,顺便说一句,你可能并不真正想要一个公共的NSMutableArray属性,如果你这样做,你可能希望它是复制,而不是保留。否则就会陷入困境。

答案 3 :(得分:0)

我知道这个问题已经被解决和接受了,但是对于那些已经或将要面对这个问题的人来说。

我遇到了同样的问题,我尝试了所有解决方案,但没有解决方案适合我。我正在进行的项目是NON-ARC。

我尝试过对属性进行简单的更改

以前我的NSMUTABLEARRAY的财产是

@property (nonatomic, assign) NSMutableArray * dataArray;

我把它改为:

@property (nonatomic, retain) NSMutableArray * dataArray;

将其从ASSIGN更改为RETAIN

它解决了我的问题