0

I'm writing a program that calculates the Jacobi algorithm. It's written in Objective-C since it runs on a Mac, but the majority is written in standard C. I'm using a two-dimensional C array and an NSArray containing 5 NSTextField labels.

The following code yields an EXC_BAD_ACCESS error:

for ( int i = 0; i < 5; i++ ) {
     NSString *resultString = [NSString stringWithFormat:@"%g", matrix[i][i] ];
     [[resultLabels objectAtIndex:i] setStringValue:resultString]; // error line
}

Any help?

EDIT

Here's where I init resultLabels:

resultLabels = [[NSArray alloc] initWithObjects:result11, result22, result33, result44, result55, nil];
13
  • You sure the is a non nil value at all possible values of martix[i][i]? Commented Apr 16, 2010 at 17:22
  • Yeah, everything gets declared as an int 0 to 9. Commented Apr 16, 2010 at 17:23
  • I think you misunderstand Squeegy's question (and Don's and paull's): split the line [[resultLabels objectAtIndex:i] setStringValue:resultString]; up into more lines and check for nil values for each object: NSArray* labels = resultLabels; id object = [labels objectAtIndex:i]; NSTextField* textField = (NSTextField*)object; [textField setStringValue:resultString]; Commented Apr 16, 2010 at 17:30
  • Weird. It errors on id object = [labels objectAtIndex:i]; even after I retain every object in resultsLabels. Commented Apr 16, 2010 at 17:38
  • Using gdb, what is the result of po labels? What is the value of i at that point? Commented Apr 16, 2010 at 17:48

3 Answers 3

1

Most likely, you are referencing an object that has been released. Is your NSArray or objectAtIndex: nil at that point? Can you show the lines where you instantiate these objects?

Sign up to request clarification or add additional context in comments.

4 Comments

paull - true; I meant memory address 0x0, not nil. Thanks for pointing that out.
Picky detail: nil and address 0x0 are the same, neither will cause a bad access error; messages to nil return nil.
I know that you can message nil, but I'm not sure why I never associated 0x0 with nil. Thanks!
I ended up going with a Objective-C based approach, but I'm accepting this answer since he was so helpful in the comments. Thanks for helping! :)
1

If you get the error on that line, then either resultsLabels is released, or the object at i is.

2 Comments

The object at i in your array is a a string, not one of your matrix of ints.
Oh sorry; you're right. Working on two projects at once gets a little confusing! ;)
1

This isn't the source of your crasher, but the %g format code is for doubles not ints; you want %d.

The items in the array would be automatically retained by the array (objects in Foundation collections are always retained by the collection), so you shouldn't need to send them extra -retain messages. So it would seem as though resultLabels may be getting released somewhere before the crash occurs.

2 Comments

Actually, I simplified. It's really a double that gets changed later on. The retainCount in the init method is 1, but it crashes when it gets called in a different method. I thought nothing happens if a message is sent to a nil object, which is why this is so puzzling.
Nothing does happen, and your comment elsewhere in this question shows that your NSArray points to 0x4. So I would guess that you have possibly assigned one of your ints to the array pointer somehow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.