phil
phil

Reputation: 1003

Why would this code crash with EXC_BAD_ACCESS during NSArray InitWithObjects

Notice in the code below, the two NSLOG statements. The "aaa" gets printed out but the "bbb" never does. Instead, the simulator crashes with EXC_BAD_ACCESS. I know that this typically means that the object I am trying to access has been prematurely released. I just can't figure out what is wrong...

Updated:

here is my .h

#import <UIKit/UIKit.h>


@interface vcAddCat : UIViewController  <UIPickerViewDataSource, UIPickerViewDelegate> {

NSManagedObjectContext *managedObjectContext;

IBOutlet UIPickerView * pickerView;
NSArray * _weights;
NSArray * _categories;
IBOutlet UILabel *lastCat;
IBOutlet UILabel *lastWeight;

}

I do not have any @property or @synthesize lines for either array...

Here are two snips from my .m

- (void)viewDidLoad {

    [super viewDidLoad];
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    _categories=[[NSArray alloc] initWithObjects: @"Homework",@"Quizzes",@"Tests", @"Mid-Term Exam", nil];
;

    NSLog(@"aaa");

    _weights=[[NSArray alloc] initWithObjects: @"1",@"2",@"3",@"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21",@"22",@"23", @"24", @"25", nil];


    NSLog(@"bbb");

..and here is where I release the arrays...

- (void)dealloc {
[super dealloc];
[_categories release];
[_weights release];
NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__);

}

Upvotes: 0

Views: 1565

Answers (5)

Jamie
Jamie

Reputation: 5122

Try going into your arguments settings for your executable and turning NSZombieEnabled to YES. This will stop your program at the exact line causing the bad memory access.

Upvotes: 0

phil
phil

Reputation: 1003

OK - loser alert.

Sorry, guys. All was working when I tested early on with only 10 objects to insert. Once I got it going, I added the rest of the objects (11 - 100) and that's when it crashed. When I posted the code above, I truncated the object list at 25 items - too redundant, it seemed.

Well, objects 26, 36, 46, etc all had a typo in them where I'd omitted the preceding '@' before the string value.

Fixed that and I'm back on track.

Thanks for your help and sorry about the lame error.

Phil

Upvotes: 6

visakh7
visakh7

Reputation: 26400

Pls change your dealloc to

- (void)dealloc {
[_categories release];
[_weights release];
[super dealloc];
}

Never call [super dealloc]; before releasing other objects of the class.

Upvotes: 0

Swastik
Swastik

Reputation: 2425

Look the problem is somewhere in your _weights array.Debug the code& check when you reach the second NSLog, whats the value of array , is it properly retained.Have you made the array as property & synthesized it

In your .h file

NSArray *_weights;

@property( nonatomic, retain ) NSArray  *_weights;

In your .m file

@synthesize _weights

Upvotes: 0

christophercotton
christophercotton

Reputation: 5879

I just put that exact code into an app I have and it ran just fine. I reached the "bbb". So, the question I have is, how have you defined _weights ? In my class I was using:

@interface MyClass : NSObject {
   NSArray* _weights;
}

Upvotes: 0

Related Questions