Mac Forums

Mac Forums (http://www.mac-forums.com/forums/)
-   OS X - Development and Darwin (http://www.mac-forums.com/forums/os-x-development-darwin/)
-   -   NSMutableArry and “EXC_BAD_ACCESS” (http://www.mac-forums.com/forums/os-x-development-darwin/166778-nsmutablearry-exc_bad_access.html)

fontdesigner 09-12-2009 07:51 AM

NSMutableArry and “EXC_BAD_ACCESS”
 
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?

xstep 09-12-2009 04:13 PM

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.

mattpb99 09-13-2009 04:53 AM

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

mattpb99 09-14-2009 09:05 AM

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.


All times are GMT -4. The time now is 10:06 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.