How would you debug this iphone bug - iphone

-[CFString respondsToSelector:]: message sent to deallocated instance 0x4b9e720
I don't where to start, tried static analyzer and stepped through each line of code but still no help.

It means you are trying to use an object which has been deallocated - i.e. sent release or removeFromSuperview:
Check this page to see how to enable a debug feature called NSZombie - it keeps objects alive even when released, and lets you know which one you are accessing. The downside is that the program uses much more memory (since nothing is truly released) but you only use it until you have found the problem then turn it off.

Adam is right. But you should also enable MallocStackLogging.
Let's assume you have this output:
2003-03-18 13:01:38.644 yourApp[**<pid>**] *** *** Selector 'release'
sent to dealloced instance **<address>** of class NSConcreteData.
Type the following command into gdb (swap and with your values):
malloc_history <pid> <address>
This will tell you where and what has been allocated.

Run it in the debugger and look at the stack trace.

Related

What other debug arguments are there besides NSZombieEnabled?

I am having issues with something being released or not retained, and Xcode will not show me exactly where the error is at.
I am working collaboratively with an individual who has a horribly organized project, and I think Xcode is even freaking out, because [CALAyer release] is thrown, but no explanation of what or where.
For env variables, you can read NSDebug.h (use open -h NSDebug.h in the Terminal). Though NSZombieEnabled is the most useful one there. If you need to debug object lifetime issues, use Instruments with the Zombies instrument. You can then see all the retain/release/autorelease events that happened for any given object.

iphone memory management strange issue

This is a piece of code I had written in xcode
Foo * myFoo = [[Foo alloc] init] ;
[myFoo release] ;
[myFoo printMessage] ;
If I am right, it should give a runtime error when printmessage function is called as myFoo gets deallocated by that time. But in xcode , the code is working and print message is getting called, is it a problem due to setting on xcode?
Regards
Abhijit
You're invoking undefined behaviour by accessing freed memory.
It might crash, it might work fine, it might result in dancing unicorns spewing forth from your nose.
To detect memory errors whilst you're developing code, you should enable NSZombie's, see instructions here:
http://www.cocoadev.com/index.pl?NSZombieEnabled
Update
You might wonder why it works like this - surely the OS should always throw an error when you try to access memory that isn't valid?
The reason why you don't always get an error (and why the behaviour is undefined) is that checking the memory is valid on every access would result in a performance penalty - ie. the code would run slower, just to check for something that shouldn't ever happen.
Hence you must be careful to trap all these errors during development, so that they never happen for an end user. NSZombies is the best tool for finding them.
One other point - if you do "build and analyze" in xcode, it might also find this error at build time. Certainly the static analyzer will detect some memory errors at build time.
Releasing an object is not instantaneous, the object will be released, but one can't be sure that it's when one sends a release message. The behavior you're experiencing is normal.

Program received signal EXC BAD ACCESS

I'm stuck with a problem in emulator. The emulator occasionally stops with
Program received signal: "EXC_BAD_ACCESS" .
as console output. No further info provided. Is there a chance to come closer to the problem?
I see that NSZombie already has been proposed, but the link doesn't seem to work anymore, so here's instructions on how to use it.
To activate NSZombie do the following:
Get info of the executable.
Go to the arguments tab.
In the "Variables to be set in the environment:" section add:
Name: NSZombieEnabled
Value: YES
Then run your app as usual and when it crashes it should tell you which deallocated object received the release message.
This often is caused by sending a message to an object that is no longer in memory. There is no error message because there is nothing on the stack when the error occurs. You can set breakpoints and step through your application until you find where the crash occurs, or you can use nszombie.
http://howtomakeiphoneapps.com/2009/02/nszombie-and-xcode-oh-my/

Debug Iphone Program received signal: "EXC_BAD_ACCESS"

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

iPhone - debugging "pointer being freed was not allocated" errors

When over freeing a pointer you may see an error such as
"pointer being freed was not allocated"
When debugging with the simulator, I add a build argument MallocStackLogging = YES - this allows me to use malloc_history in the terminal to track down where I have over freed a pointer.
If I debug on the device with this build argument I get all sorts of console errors "cannot create stack log files" etc.
Oddly, I get some over freed pointer errors appearing on the device, but not on the simulator.
Has anyone had any experience tracking these down using the device itself?
Thanks!
Another way to do this. Make sure to turn NSZombie on so it reports the memory address of the object that is getting the extra release. Then Run with Performance Tool->Object Allocations. This will bring up instruments. Look at the Console log as provided by Xcode organizer. Once you get the crash lookup the memory address in instruments. You will see the entire history of mallocs/frees on that object, as well as links straight into your code.
I generally use NSZombie for such things, check this out
You need to set the MallocStackLogging env variables on the target executable...
To access these settings, select your executable from the Groups & Files pane in XCode, then Get Info.
Go to the Arguments tab and add the following entries into the “Variables to be set in the environment” box:
Please test the program for memory leaks,Also check autoreleases and whether you are releasing objects properly or not.Also we need to check whether a released object has a memory allocated or not.You also need to be careful regarding autorelease,because accidentally we might release an array or a string or any object that is already autoreleased...hope it helps and works!
Tip: You can test for leaks by analyzing your project(click shift+command+k)