Debug Iphone Program received signal: "EXC_BAD_ACCESS" - iphone

My iphone app randomly received this message. I know certain it is memory release problem. However what is the best way to find which object leads this problem. Here are what I have tried
Use Instrument Leak and
ObjectAllocation Trace. Dont saw any
help to know which object have this
problem
Put NSZombieEnabled=YES and project executive ... Dont saw any
help either
Put NSLog everywhere but the EXE_BAD_ACCESS just appear anywhere.
in the debuger, just saw the code
happened in the assembly. like
objc-msg send.
review code many times and read memory management a lot time
and research online a lot time. but
no surprise.
Is there a completed solution to figure out this problem easily. I am a previous Visual C++ programmer, I deal with memory management with years and it is easy to debug and figure out in Visual C++.

If you couldn't see any helpful debug info, I would suggest you find all the places that you are doing a release. It is most likely the case that you have released something that did not need to be released. Code would help us in tracing the issue with you.

As Juan noted, the first stop is the Debugger - what does the debug window give for a stack trace when the app crashes? You should be able to see the line it crashed on... you said in a comment to one response that you saw the crash happen around the lines:
CGPDFDocumnetRef docA=CGPDFDocumentCreatWithURL(myurl);
CGPDFDocumnetRef docB=CGPDFDocumentCreatWithURL(myurl);
Are you really using the same URL object for both calls? Which line is it exactly?
It could be something around the way you make use of the CGPDFDocumentRef, you can find example code how Apple uses them in the QuartzDemo project, file "QuartzImageDrawing.m" (you can find the demo project from the developer portal or embedded in the iPhone documentation with XCode).
XCode is actually pretty powerful, but it does things differently from other IDE's.

In addition to Erich answer, I'd want to add go backward. Start with the most recently added release and work from there.
I ran in to this and it turned out I was releasing an auto-released object that was returned from a convenience method built in to the Cocoa-Touch framework. My problem was as Erich described -- I released this auto-released object. When the system attempted to release it, the program gave the error you are describing.
Regards,Frank

The best way to know what happend is using the xCode Debbuger, give it a try.

You will also receive the message when you don't pass enough parameters to a variable argument method. For example having a NSLog statement like this: NSLog(#"Hello %#");

To check what the error might be
Use NSZombieEnabled.
To activate the NSZombieEnabled facility in your application:
Choose Project > Edit Active Executable to open the executable Info window.
Click Arguments.
Click the add (+) button in the “Variables to be set in the environment” section.
Enter NSZombieEnabled in the Name column and YES in the Value column.
Make sure that the checkmark for the NSZombieEnabled entry is selected.
found this on iPhoneSDK

Related

XCode 4 - IPhone Dev - Good Debug Tutorial

I'm beginning IPhone development with XCode 4 and Objectiv-C. Since I think strong debug and IDE knowledge is required to master a language, I'm looking for good resources about the debugging process and XCode.
A problem I faced recently was that, an unknownException was being thrown in a sample code and I couldn't know how to debug it, since it crashed right after jumping into my Main. I think with a better configuration, it would have been much easier to debug !
I've already found this resource : https://developer.apple.com/library/ios/navigation/
But I couldn't find something related to debug in there... Did I miss something ?
Andy, this first is about super basic debugging: http://mobile.tutsplus.com/tutorials/iphone/xcode-debugging_iphone-sdk/
And here you can find two excellent tutorials:
http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1
http://www.raywenderlich.com/10505/my-app-crashed-now-what-part-2
Easy to make mistakes in Objective-C...
Not having a view attached to your view controller
Sending a message to a deallocated instance. Turn on NSZombieEnabled.
Forgot to add the Framework (e.g. MapKit)
If the crash isn't fixed at this point. Set a breakpoint in your app delegate and just step through every line until it crashes. Move the breakpoint up, run again, and inspect the values.
Apple has a list of what they consider to be the best debugging tricks for iOS here.
There is also a couple of videos on the developer's site. I can never find out how to make a direct link, but there aren't that many videos, so just search for "debug":
https://developer.apple.com/videos/wwdc/2012/
There is also one from last year:
https://developer.apple.com/videos/wwdc/2011/
Access to the developer site requires registration.
Should you ever consider venturing into OS X territory, then here is the Apple-approved debugging magic.

Adding code to class files just before building - Objective C

I am looking to develop a framework, for which I dont want get into details.
Suppose if I am having 100+ classes with 1000+ methods in my iPhone application.
In this scenario I want to add NSLog in each method(at start or end or both) of each class.
Manually adding NSLog is possible but is there any better solution?
Like building application in such a way that I can add this Log without me having to do manual work.
Thanks and Regards,
Denis,
Your answer was most useful throughout many forums.
Thanks a lot.
The reason I'm looking for something like this is, we have many client projects and many time it happens that we get some crashing or other issues while QA and UAT. ~80% of them are not reproducible or require some particular scenario. We were using .crash and dsym to track such issues. But it is not that useful in such scenario.
What I'm looking for is providing add hoc build which will log the steps which user has followed so that it will make easy to reproduce such issues.
Currently I am using precompiled headers and first method you have mentioned (searching for the first opening brace then replace it with macro).
I will look into DTrace and objc_msgSend as you mentioned. I will google out these meanwhile if you have any preferred tutorials it will be great.
Thanks and Regards,
:D
So you want to add a trace facility to your code?
Apple doesn't provide anything like this. You'll have to add your trace facility yourself. If your source code style is consistent, this might be relatively easy to do automatically, something like searching for the first opening brace following a line starting with a minus (or plus) sign...
Alternatively, you might want to use the public Objective-C runtime functions to enumerate all classes and all their methods, then method-swizzle each of them with another one that NSLog before jumping to the original.
Alternatively, you can take the open source implementation of objc_msgSend and insert a call to NSLog at the beginning. Note that obj_msgSend tail calls into the method, so that will prevent you from adding a call upon return. Be prepared to humongous output of course. You might want to condition your call to NSLog to the value of the selector parameter to objc_msgSend (such as a common prefix).
Finally, the best way to trace is probably to attach a DTrace probe to the entry to objc_msgSend. For the same tail-call reason mentioned above, you won't be able to attach a DTrace probe to its return though.
But the better question is why do you want to do that.

autorelease and retain causes message sent to deallocated message

I've been getting this random crash, in which I don't know how to reproduce again. It happens when I was browsing/using the apps for some time, switching views here and there and I got this:
First of all I don't know which UIImage is this crash referring to. I do have a crash report, but I don't know how to read it. I would like to reproduce the error, but I don't know how to.. it seems that it's quite random. Can someone help me out in debugging this?
Hard to say something for sure. This is my guess.
I see you are getting some data from an external source "convore.com". When you say you get the error randomly, I think it may be due to the data received from the convore.com api. Probably from time to time you are not getting the image, link, or graphics from the server and you don't create and image, and then you are trying to retain it, causing the error.
Without being able to reproduce the failure, it will be very hard.
If you cannot reproduce it, I would suggest building your project by using the Xcode option "Build and Analyze". Indeed, since this is seemingly a problem with retain/release, "Build and Analyze" could give you some hint at where things go bad. By the way, if you are not doing it, it is highly suggested, almost mandatory check.
You could try and post the crash report, but I doubt it will be useful. The point is finding out where the UIImage is deallocated and why, the crash report refers to an attempt to use the UIImage after its release, so this would likely be in a different portion of your code.
If you have some suspect as to which class contain this UIImage, you can post some code so that we can help you inspect it.
As a hopefully useful hint as to how to reproduce this issue, I would also suggest sending the simulator a Memory Warning. Indeed, in such cases all UIViews that are not displayed are released and this could be the culprit with your UIImage. So, you send it and see if the crash happens again... just a hint, anyway...

What does "dwarf2_read_address: Corrupted DWARF expression." mean?

I'm trying to do a little debugging in my iPhone project, and whenever i try to print out an object to see its description in one specific method i get this message in gdb:
"dwarf2_read_address: Corrupted DWARF
expression."
Does anyone know why this happens and how i would go about fixing it so that i can inspect my variables?
Apparently this is a known bug in gdb, fixed in later versions (6.7 and above). I've found a reference to it in this bugzilla thread, but that's all I managed to find that's in any manner useful. Try updating your GDB installation.

How NSZombie works when it finds error on iPhone SDK

if set to on the NSZombie, it tells me that I'm releasing an object which is already released and gives me the memory address, very helpful... BUT I can't see which object it's... because the app stopped working...
any help?
thank you
You are looking for: malloc_history
http://www.friday.com/bbum/2010/01/10/using-malloc-to-debug-memory-misuse-in-cocoa/
malloc_history and my post on the same is certainly a correct answer and one way to do this.
However, it is quite likely you can do the same with Instruments, which is also going to be a bit less low level and more straightforward. Under Xcode's Run menu is Run With Performance Tool. Under that, you'll find Zombies.
Running with that, you should be able to see the type of the object being messaged and the retain/release event history.
Try Apple's http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/Introduction/Introduction.html utility, found in /Developer/Applications/.
A commercial alternative is http://www.omnigroup.com/developer/omniobjectmeter/