NSMutableArry and “EXC_BAD_ACCESS”

Joined
Sep 11, 2009
Messages
1
Reaction score
0
Points
1
Could somebody help me?

I declared NSMutableArray in header file like this:
TPEditGlyphView.h:

@interface TPEditGlyphView : NSView
{
...
NSMutableArray * observedKnots;
....
}

Next I try to use one, but any access makes "EXC_BAD_ACCESS"..

TPEditGlyphView.m:
//only places where observedKnots appears
.....
-(void) awakeFromNib
{
.....
observedKnots = [NSMutableArray arrayWithCapacity:0]; //CRASH, EXPLOSION...
}

-(void) observeValueForKeyPath: keyPath ofObject: object change: change context:context {
if (context == TPKnotsSelectionDidChangedContext) {
for (TPKnot *knot in observedKnots) { //CRASH, EXPLOSION...
....
}
observedKnots = [NSMutableArray arrayWithCapacity:0];
for (TPKnot *knot in [knotsController selectedObjects]) {
....
[observedKnots addObject: knot];
....
}
}
....

Where could be bug?
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
Do you have the foundation kit imported, as in;

Code:
#import <Foundation/Foundation.h>

I did try the observedKnots declaration with the assignment statement and it worked fine for me.
 
Joined
Sep 13, 2009
Messages
2
Reaction score
0
Points
1
The order you declare variables in your .h file may be the problem. in a project I was working on today I found that
Code:
double var_price[17];
NSString *var_name[17];
int var_stock[17];
NSString *var_itemcode[17];
int var_maxstock[17];
int var_arrayno;
returned the EXC_BAD_ACCESS error but after changing the order of things the error went away.
Code:
NSString *var_name[17];
NSString *var_itemcode[17];
double var_price[17];
int var_stock[17];
int var_maxstock[17];
int var_arrayno;
I have no idea why reordering things fixed the problem and it took me hours to find it in the project. The idea "hmm, maybe the variables are declared in the wrong order in the header file" isn't the first thought that came to mind and theoretically I would not think it would make a difference but you can't argue with results ;D
 
Joined
Sep 13, 2009
Messages
2
Reaction score
0
Points
1
Just worked out what was causing my problem yesterday, it seems that if you declare
Code:
someobject* anarray[2]
this in objective C (correct me if i'm wrong) means the array size is 2
Code:
anarray[0] //valid
anarray[1] //valid
anarray[2] //invalid
This caught me out because I program in vb and the last one would be valid in that language. The data that was being written outside of the array was in fact being written to the first memory location of the next array hence when the program tried to read something from the second array and information from the double was in the NSString array the program crashed. However I then after rearranging the variables found strings from the end of the first array popping up in the beginning of the second not crashing the program as they were both the same data type.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top