Break on EXC_BAD_ACCESS in Xcode?

I am new to iPhone and Xcode development in general and have no idea how to start troubleshooting the EXC_BAD_ACCESS signal. How can I get Xcode to break into the exact line causing the error?




I can't get Xcode to stop at the line causing the problem, but I see the following lines in my debug console:

Sun Oct 25 15:12:14 jasonsmacbook TestProject [1289]: CGContextSetStrokeColorWithColor: invalid context

Sun Oct 25 15:12:14 jasonsmacbook TestProject [1289]: CGContextSetLineWidth: invalid context

Sun Oct 25 15:12:14 jasonsmacbook TestProject [1289]: CGContextAddPath: invalid context

Sun Oct 25 15:12:14 jasonsmacbook TestProject [1289]: CGContextDrawPath: invalid context

2009-10-25 15: 12: 14,680 LanderTest [1289: 207] *** - [CFArray objectAtIndex:]: message sent by deallocated instance 0x3c4e610

Now I'm trying to draw in the context that I am extracting from UIGraphicsGetCurrentContext() , and go to the object that I want to draw with.




Further trial version and error debugging, and I found that NSMutableArray I have a property for my class - zombies. I entered the init function for the class, and here is the code I used:

 if ((self = [super init])) { NSMutableArray *array = [NSMutableArray array]; self.terrainBlocks = array; [array release]; } return self; } 

I deleted the [array release] and no longer gave me the EXC_BAD_ACCESS signal, but now I'm confused about why this works. I thought that when I used the property, it automatically saved it for me, and so I have to free it from inside init so that I don't have a leak. I am completely confused about how this works, and all the Stackoverflow guides and questions that I read only confuse me more about how to set properties in my init method. There seems to be no consensus as to which way is the best.

+45
debugging objective-c iphone xcode exc-bad-access
Oct 25 '09 at 21:11
source share
10 answers

For any EXC_BAD_ACCESS errors, you usually try to send a message to the issued object. The BEST way to track these actions is used by NSZombieEnabled .

This works by never releasing any object, but completing it as a โ€œzombieโ€ and setting a flag inside it that says it would normally be released. Thus, if you try to access it again, he still knows what it was before you made a mistake, and with this little information you can usually backtrack to find out what the problem is.

This is especially helpful in background threads when the debugger sometimes tricks into any useful information.

IT IS VERY IMPORTANT TO NOTE , however, you need to be 100% sure that this is only in your debugging code and not in your distribution code. Since nothing is ever released, your application will flow, flow and flow. To remind me to do this, I injected this log into my appdelegate:

 if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled")) NSLog(@"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!"); 

If you need help finding the exact string, do Build-and-Debug ( CMD-Y ) instead of Build-and-Run ( CMD-R ). When the application crashes, the debugger will show you which line, and in combination with NSZombieEnabled, you should know exactly why.

+84
Oct 25 '09 at 21:15
source share

About your array. Line

 NSMutableArray *array = [NSMutableArray array]; 

doesn't actually give you a stored object, but rather an autorelease object. It is probably stored on the next line, but then you should not let it go on the third line. See this

This is the basic rule:

You get ownership of the object if you create it using a method whose name begins with "alloc" or "new" or contains "copy" (for example, alloc, newObject or mutableCopy), or if you send to save the message. You are responsible for refraining from owning your own property through release or auto-advertising. At any other time, when you receive an object, you should not let it go.

+17
Oct. 25 '09 at 23:04
source share

In Xcode 4, you can enable Zombies by clicking on the Scheme drop-down list (top left, next to the stop button) โ†’ Edit Scheme โ†’ Diagnostics tab โ†’ Enable Zombie Objects

+10
Nov 10 '11 at 12:37
source share

Xcode / gdb always breaks on EXC_BAD_ACCESS , you just need to work on the call stack to find the code that called it.

Note that such errors often occur with autoreleased objects, which means that the final cause of the problem will not be in the call stack that caused EXC_BAD_ACCESS . This is when NSZombieEnabled and NSAutoreleaseFreedObjectCheckEnabled become useful.

+7
Oct. 25 '09 at 23:01
source share

A new answer to the old thread ... in XCode 4, the most effective way to diagnose EXC_BAD_ACCESS exceptions is to use tools to profile your application (from XCode, click "Product / Profile" and select "Zombies"). This will help you identify messages sent to freed objects.

+5
Nov 15 '11 at 11:24
source share

From the Stanford CS193P classes: if you added a breakpoint (manually by editing breakpoints) for the objc_exception_throw character, you can get a much better idea of โ€‹โ€‹what went wrong - letting everyone jump to the point where the debugger stops itself tends to obscure things and ruin the stack trace. When you stop at objc_exception_throw, you can often look back at what access / operation caused your problem.

+2
Oct. 25 '09 at 10:23
source share

Another useful approach is breakpoints that will start immediately after an exception occurs:

Open the Breakpoints window (Run - Show - Breakpoints) and add two symbolic breakpoints called "objc_exception_throw" and "[NSException raise]"

From: http://blog.emmerinc.be/index.php/2009/03/19/break-on-exception-in-xcode/

+2
May 23 '10 at 4:39 a.m.
source share

I just wanted to add for those who come from the Internet, looking for solutions for the same error, but with a different error. In my case, I got the same error when I tried to create an NSDictionary with a typo in the key name, where I forgot to add the "@" in front of my key:

 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: myObj1, @"goodKey", myObj2, "badkey @ is missing in front", nil]; 
+1
Aug 13 '11 at 11:59
source share

I hope I did not miss the identical answer, but I found that some projects may throw this error due to running on simulators for older versions of iOS, which may not be compatible with the project dependency or structure. I chased one of them for too long before realizing that this only happens in versions of large simulators such as the iPhone 4S, the application should not even try to support it.

It would be nice to get a more detailed error message, but I would suggest that the responsibility for the structure in which it arose ... Anyway, this is a pretty common premise for searching, and maybe it will help someone goofing so much as I found myself.

+1
Sep 23 '16 at 6:29
source share

Before turning on the zombies, I recommend that you first get rid of all warnings (if any). Simple things, such as non-empty functioning without return , can cause this error. Unless you have warnings, as other answers say.

0
Mar 26 '14 at 4:55
source share



All Articles