No ARC causes crash in TahDoodle - how to tell if ARC is on?


#1

I typed out the example exercise TahDoodle perfectly but it crashed every time I clicked Insert. The error messages weren’t very helpful, and setting various breakpoints, stepping through my code didn’t shed much light either. Finally I found someone else with the same issue in another thread - and the problem was ARC was not turned on, and the todoList array being deallocated early. To test this, Aaron advised to throw in a [todoList retain] which would #1 - fix the problem by retaining the array and #2 - verify if ARC was on or off (if ARC was on, it should cause an error).

So my TahDoodle works perfectly now by adding the [todoList retain] line, but my question is this : Is there an easy way to tell if ARC is on or off? I don’t remember being given a choice when I created the project. When I google “How to tell if ARC is enabled?” or “How do I tell if I am running ARC in Xcode” you get no answers. Is there somewhere you can check in the build settings or preferences? I seem to keep running into this problem.

How can I easily tell if ARC is on or off??

Thanks!


#2

ARC is a checkbox during the project creation wizard (except perhaps in CLI C-only projects, don’t recall off-hand).

If you’re not seeing that, you’re probably not running Xcode 4.2 (or greater) under Lion.

If you are running the necessary Xcode and OS X releases, visit the project build settings (click on the project name in the navigator - with the blue icon) and look for the “Apple LLVM compiler 3.0 - Language” section; you should see Objective-C Automatic Reference Counting.


#3

Thanks, I found it, exactly where you said in “Apple LLVM compiler 3.0 - Language” buried at the end of Build Settings. ARC was set to “No”

Also, this jogged my memory from the last time I ran into this - If you’re running OS X 10.6.8 (snow leopard) with XCode 4.2 then you can only use ARC for iOS (Cocoa touch) apps, but not for Mac (Cocoa) apps. I remember reading an article explaining this limitation. And because TahDoodle is a Mac application, not iOS, that’s why I was never given the option for ARC during project creation.

Just for kicks, I tried turning ARC on anyways, but then when I try to build & run I get Apple Mach-O linker errors about files not found etc.


#4

I am running 10.6.8 and XCode 4.2 and getting the same problem. Could you please explain “adding the [todoList retain] line” a bit further?

Thx,
–bd–


#5

I’m by no means an expert, but here’s the way I understand this:

When you click the button “Insert” in your program, it calls the createNewItem: method.

The createNewItem: method checks to see if an array list named todoItems exists, if not it creates one with the line : todoItems = [NSMutableArray array]; and then adds an object to the todoItems array with the line : [todoItems addObject:@“New Item”]; So now you have an array object named todoItems in memory that contains your important data.

The problem is that if nobody “owns” this object, it will be deallocated at some point in the future. How do you know if somebody “owns” the object? Have you or another object ever sent it the “retain” message? If not, nobody owns it. So the problem is that since nobody owns todoItems, then as Aaron puts it, the object continues it’s death march to deallocation. And when the object gets deallocated, it’s gone from memory and when the program wants to use it again (ie - to display the To Do List) it’s not there and causes the program to crash!

When you send the retain message to the todoItems object - [todoItems retain], it increases it’s retain count by 1 and ensures that the object will stick around forever, until you specifically send it the release method - [todoItems release] In this program you would never send [todoItems release] because you always need this object for the program, as it holds the entire contents of the program and the program can’t run without it.

At first I found this entire concept REALLY confusing. And apparently memory management is the #1 stumbling block for people learning Objective C. This mess is what ARC fixes. With ARC (automatic reference counting) you don’t need to worry about retain and release because XCode does it all for you. However ARC isn’t supported on OSX 10.6 Snow Leopard with XCode 4.2 for Mac apps, only iOS apps. On OSX 10.7 Lion with XCode 4.2, ARC works on both Mac apps and iOS apps.

I hope that helps - like I said at the top, I’m not an expert, but this is my understanding of it. If you have this book in e-format like Kindle, do a search for the phrase “Death March” and re-read that chapter again after 2 or 3 cups of coffee and all will become clear.


#6

I implemented your fix and it worked perfectly. Now, would I have to release the todoItems array?


#7

I don’t think you should ever release the todoitems as this will crash the program when you want to add a todoItem, not so?

My advice is if you’re serious about developing for the latest iOS OS - upgrade your machine to Lion or ML with the latest version of Xcode. Turn ARC on and it’s one less thing you need to worry about.


#8

Apologies for re-opening an older thread but this is exactly the problem I have just encountered. Not due to running an older version of xcode but due to my error in not clicking the tick box to enable ARC. After a few hours of trying to find the solution I realise I would never have spotted this.
So my follow up question is how would I have found this error?