Program received signal: “EXC_BAD_ACCESS”


#1

I’m using xcode 3.2.4

following the instructions of the book my app throws the error in subject.
I fixed it changing this line

with this

now it works like a charm :sunglasses:

hope it might be useful

sorry for my english


#2

The 4th Edition is intended for use with Xcode 4.2 on Mac OS X Lion (10.7), with ARC, so under ARC the line

…is fine. Under manual retain counting, which Xcode 3.x uses, the voices array must be retained. Your solution works, but usually we would accomplish that by sending -retain to the array:

…and remember to send -release to it when we are done with it. This is only if you are not using ARC, however.

Adam


#3

I ran into the same problem with Xcode 4.1 and Lion (10.7) - after the update (Hint: The App-Store doesn’t make an autoupdate!) had to enable ARC first:


Uploaded with ImageShack.us

btw, wouldn’t it be easier/better to call the class method instead of the instanz method? - like this:

For me as beginner it’s really confusing, that there are three possible ways to reach the same result. Could you give me an idea about the differences ?
Using retain here was new to me - so retain take care of allocation and initialization too?!

Happy Xmas!
Vertex


#4

Vertex,

(The following assumes that you are not using ARC.)

The array returned by [NSArray arrayWithArray:…] is retain-autoreleased, so it would have the same problem as storing the return of availableVoices: at the end of the current run loop cycle, it would be autoreleased, leaving a bad pointer.

The array returned by [[NSArray alloc] initWithArray:…] is retained (+1 retain count), thus it is not autoreleased. Sending an object the -retain message increments its retain count.

Check over Chapter 4 for more on this, specifically the Retain Count Rules on page 76.

Adam


#5

I know but I’m also following iPhone Programming 1 edition and I want to finish that using the right SDK :slight_smile:

thank you so much for your answer and for the advice.

great books and great forum! it’s very helpful!


#6

Thanks Adam for the quick response,

[quote=“AdamPreble”](The following assumes that you are not using ARC.)
[…] Check over Chapter 4 for more on this, specifically the Retain Count Rules on page 76.[/quote]
I will read this chapter again, I think memory management is a very important issue.

btw, i found by chance an interesting discussion at “stackoverflow” about which method you should call, when using ARC.
It’s exactly my question :smiley: I hope it’s ok, that I link it here.

cu
Vertex


#7

I had the same issue - but I was using XCode 4.1

Really glad to find the solution here. Thanks for your efforts Adam.

I’ve had to upgrade to version 4.3 - will this present any serious issues?

Sam


#8

FWIW, I also had the exc_bad_access error in this line:

After turning ARC on, and commenting out the dealloc code, the app ran fine.

Xcode 4.3/10.7/4th edition

  • Koen.