New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus • Advice and insight from world-class Apple enthusiasts • Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

NSMutableArry and “EXC_BAD_ACCESS”


Post Reply New Thread Subscribe

 
Thread Tools
fontdesigner

 
Member Since: Sep 11, 2009
Posts: 1
fontdesigner is on a distinguished road

fontdesigner is offline
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?
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks
mattpb99

 
Member Since: Sep 13, 2009
Posts: 2
mattpb99 is on a distinguished road

mattpb99 is offline
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
QUOTE Thanks
mattpb99

 
Member Since: Sep 13, 2009
Posts: 2
mattpb99 is on a distinguished road

mattpb99 is offline
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.
QUOTE Thanks

Post Reply New Thread Subscribe


« how to set my path? | pow in objective-c »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

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

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

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?