Code crash


#1

I’m sure I’m doing something really stupid, but having followed Chapter 20 through to page 291, my app crashes when I hit any key. More specifically, I have the custom view selected (focus ring showing), and I press a key. Instant crash at the first line in drawStringCenteredIn: ("Thread 1: Program received signal: “EXC_BAD_ACCESS”).

  • It appears that there is a retain count issue with both the “string” instance variable, and the “attributes” variable.

If I enter: [string retain] in setString:, and [attributes retain] in prepareAttributes:, then all is well with the world. Not sure why I should need to do this, however, unless the operating system is moving memory around underneath me.

Have I done something really stupid to warrant this, or is there a problem with the code in the book?
Thanks!


#2

The obvious question is: Was everything working up to chapter 20?

I ask this, because this TypingTutor continues to be used for the next several chapters and it evolves a lot. The point being that it relies on everything working at each point along the way.

In any case the error you are seeing should be impossible to fix with the solution you used as [color=#0080FF][foo retain][/color] is an illegal operation under ARC. In other words the Xcode should be objecting loudly to you entering that line and it should refuse to compile. Check your ARC settings by clicking on the TypingTutor entry at the top of the Project Navigator (far left column, top entry; far left choice on the toolbar). In the center portion, choose “Build Settings”, “All” and “Combined”; then in the search field, type “Auto”. This should reduce the clutter to a few items. Make sure that the “Apple LLVM compiler 3.0 - Language” area has “Objective-C Automatic Reference Counting” set to “Yes”.

Go back into your program and remove the [foo retain] statements and build it again.


#3

OK, so everything was working fine until this point in Chapter 20.

  • I’m not quite sure about the ARC settings that you are referring to. I think this is a red herring.

I also own Edition 3 of this book, and after posting my first message, I went back to this edition and saw that the code for chapter 20 is quite different. For example, there is a dealloc method supplied, and these variables are retained. If I use the Edition 3 code, then everything works just fine. I suspect that there are some serious typos in Edition 4.

(I have just switched over from Edition 3 to Edition 4, so it could be that I’m using the “wrong mindset” here - I’m happy to be corrected. However, I did follow the instructions in Chapters 19 and 20 to the letter, and the app still crashed once I got to the stage at page 291.)


#4

For the record, I have Automatic Reference Counting turned off. I didn’t do this explicitly: that just seems to be the way that Xcode is set up. If this is the reason the code crashes, then I think the authors should make it explicit, in their instructions in Chapter 19, that their code does require some arcane editing of build settings.


#5

FastJag,

The 4th edition of the Cocoa book is written for use with Xcode 4.2, and is all ARC, with the one exception of Chapter 3 where we discuss manual retain counting. This is probably the reason for your crashes. You can still use the 4th edition for APIs and principles, but if you don’t want to use ARC, when following the examples you will need to be careful to adjust what you see to follow manual retain counting rules. The new editition of the book doesn’t any longer, due to ARC.

Adam


#6

[quote=“FastJag”]For the record, I have Automatic Reference Counting turned off. I didn’t do this explicitly: that just seems to be the way that Xcode is set up. If this is the reason the code crashes, then I think the authors should make it explicit, in their instructions in Chapter 19, that their code does require some arcane editing of build settings.[/quote]Actually the default for Xcode 4.2 the very first time you ran it and started a project was to have Automatic Reference Counting to be enabled. If you made a project with it not enabled (removed the check mark during the project definition), then it will remain unchecked until you restore it to the default during a project definition. Even the project Adam referred to in his reply (Chapter 3) starts out as as an ARC project, see Figure 3.2 on page 38.

The “arcane” instruction I provided was the easy way to put this project back into ARC mode since apparently you did not start the project with ARC enabled. The alternative is go back to the beginning of Chap 19 and restart the project making sure that the ARC check box is checked. :slight_smile:


#7

Thanks for the suggestions. I appreciate that anyone starting “from scratch” with Xcode 4.2 and with only this edition, should be fine.

  • The problem is with those of us who have been long-term users of Xcode since the start. We won’t have “clean” settings. Furthermore, those of us trying to switch from Edition 3 to Edition 4 will have to readjust. I’m not complaining about this - just flagging it. I think it would have helped greatly if the Preface had emphasized the differences between editions more, and if at the start of each new project there was a bit more explanation of the build settings required:-

  • It should be possible to pick up a book and follow the example, and have that work. This is not necessarily the case with Chapters 19 and 20 (since not all of your readers will be starting completely from scratch with a “clean” Xcode 4.2 system).

Given that the authors are clearly trying to bundle as many new technologies into this edition as possible, they do need to emphasise these a little stronger.


#8

[quote=“FastJag”]Thanks for the suggestions. I appreciate that anyone starting “from scratch” with Xcode 4.2 and with only this edition, should be fine.[/quote]You’re certainly welcome; however…

[quote=“FastJag”]- The problem is with those of us who have been long-term users of Xcode since the start. We won’t have “clean” settings. Furthermore, those of us trying to switch from Edition 3 to Edition 4 will have to readjust. I’m not complaining about this - just flagging it. I think it would have helped greatly if the Preface had emphasized the differences between editions more, and if at the start of each new project there was a bit more explanation of the build settings required:-

  • It should be possible to pick up a book and follow the example, and have that work. This is not necessarily the case with Chapters 19 and 20 (since not all of your readers will be starting completely from scratch with a “clean” Xcode 4.2 system).

Given that the authors are clearly trying to bundle as many new technologies into this edition as possible, they do need to emphasise these a little stronger.[/quote]IMHO this book requires that you read the entire book. Skipping sections because you feel you understand Objective-C, Cocoa, or Xcode or if you have already read previous editions will cause you problems. I direct your attention to page 4 paragraph 2 and the beginning of the very first project on pages 12 & 13. You may not have skipped sections, but clearly you failed to grasp the points indicated by the references I point out here.

Your suggestion that folks upgrading would not start with clean settings is specious, I started my trek into OS X programming just this past summer (coming from the PC world), starting with Xcode 3.2 and fighting the upgrade to 4.0. Since even Apple’s sample programs were being adapted to the newer Xcode I realize I would have to follow suit. I started with 4.1 and moved to 4.2 when it became available and avoided using ARC; although I wanted to move to it, I was having a hard time understanding the concept. This book and Aaron’s “Objective-C Programming: The Big Nerd Ranch Guide” were the first two books out covering Objective-C & Cocoa using Xcode 4.2 and ARC specifically and that is the reason I bought both of them.

Since I did read this book from the front to where I am now (moving into Chap. 28) and I didn’t seem to experience the problem that you did, I clearly understood that you missed an important concept. All I attempted to do was give you the easy solution to fix your current problem, use it or not. It makes no difference to me, but since you are the one who had the problem and came to this forum for help, it would seem to me that you would take the solution as presented. Trying to shift the blame for your error onto the authors says more about you than anything you missed in the book.