Understand NSZombie message - objective-c

I enabled the nszombie in xcode 4.2. (from product->editScheme)
I have got this message:
-[buttons respondsToSelector:]: message sent to deallocated instance 0x48ae50
I do have buttons class, but i dont see what the exact problem by this message.
can i get more from the NSZombie ?

The problem is that an object of type buttons was deallocated and then it's respondsToSelector: method was called, that object is at address 0x48ae50.
If you were running without NSZombies your application would have crashed.

What this specific message means is that the buttons instance (at 0x48ae50) has been deallocated (a.k.a. released). So, by trying to send the respondsToSelector: message to nothing (remember the instance has been de-alloc'd), it throws an error.
Try setting a breakpoint near the place where it's being triggered and see why... (for now, your buttons object will most like have an address of 0x0)
In most (if not all) cases, this has to do with some faulty memory management; an object being released too soon, not retained when it had to, etc... ;-)

Related

An Objective-C message was sent to a deallocated object (zombie) at address: 0x75d52a0

I am relatively new to iOS development so appreciate your help in finding out the root cause for the error that I encountered.
I tried to debug the error using Instruments (i.e. Allocations-Zombie Profiler) but I could not make sense of the call stacks that were presented.
This is how the user interfaces are linked:
TabBarController -> NavigationController -> TopPlacesTableViewController -> RecentPhotosTableViewController -> PhotoViewController
The error occurs when I click on the Back button in the last view (i.e. that of the PhotoViewController). This action is supposed to show the previous RecentPhotosTableViewController but instead an unknown deallocated object was accessed, sometime in between the events of viewWillAppear and ViewDidAppear.
Additionally, I have a GenericTableViewController which is the parent of TopPlacesTableViewController and RecentPhotosTableViewController. The children set a NSMutableArray property in the parent which is the data that gets loaded in the children's views.
I am currently using iOS6 and XCode4.5.
[Update: In the Console, this line was shown - "[UIView _forgetDependentConstraint:]: message sent to deallocated instance xxx"].
I feel you are not using ARC, and you are not retaining of passing your previous object. In the meantime the previous object is released and then you accessing it.
Either you can refactor your code to use ARC or put retain or autorelease.
Go to Product > edit scheme >Diagnostics tap then check on enable Zombie objects
make a break point and go step by step to know which object is deallocated, it perhaps the pointer to your object has been removed then the OS has deallocated your object.

unrecognized selector sent to instance on deviceOrientationDidChange

Right now my app should only supports Portrait. On Summary/Supported Device Orientations I have only selected Portrait so I'm hoping that my app will not rotate. I was testing the app on a device and suddenly I'm getting the following error randomly:
[UIButtonContent deviceOrientationDidChange:]: unrecognized selector sent to instance
It happens when I rotate the device SOMETIMES, is not consistent, and is not always over UIBUttonContent. I supposed that if I only select Portrait, deviceOrientationDidChange should not be called or should be ignored.
Other times my app crashes with an EXC_BAD_ACCESS (code=1, address=something) but it happens when I rotate the device so I'm guessing that both errors are related.
I don't know what to do with this, it's hard to debug because I don't have feedback, the All Exceptions Breakpoint is not being called, so I don't know where and exactly why this is happening. Any idea on how to debug this is welcome.
These are the classic signs of a memory management error. You have over-released some object and it has been deallocated while something else still references it. Later, something messages it. In some cases a new object has taken its place, but that object doesn't understand the messages it's receiving. In other cases, there's no valid object and you get a crash.
Edited to second the advice to use the Zombies instrument to find the over-release.
Do you have a class that should be called with deviceOrientationDidChange:? When this happens, it usually means that you have a dangling reference to a deallocated object. You should try profiling your app with Instruments in "Zombies" mode.
I solved this issue a long time ago, but I think is good to share what actually helped me on this case.
After trying everything with no results with Instruments I started debugging old-school. I had an idea of "where" the error was so I just commented all the code on that section. I was right, the bug just disappeared along with some functionalities. After that I made "binary uncommenting" (uncomment one half) till I got the bug line. It was a third party library, I had an object that was not being released properly.

Instruments does not show me the real reference count

I am attempting to debug an issue where an object is released too many times and is then deallocated by the event loop. The next time I try to read from the object, the application crashes with EXC_BAD_ACCESS.
To debug, I am enabling NSZombies and then using the Zombies profile with Instruments. I then reach the point where my app crashes and Instruments informs me about a message being sent to a deallocated instance. So far, so good.
It does a very good job of showing me where libraries such as UIKit are releasing and retaining the pointer to my object, but it does not show me where my own code is doing this. It is also not displaying the true reference count. By the time that Instruments says that the reference count is 1, the reference count is actually 8 according to printing out the reference count in gdb.
I have investigated the possibility that Instruments is somehow filtering the output but it would appear that I really am recording every release and retain event. It absolutely must be something in my application because my view controller is instantly deallocated after displaying, meaning that attempting to do anything that would send a message to the controller would crash it.
After placing a breakpoint in the dealloc method, I found that my object is indeed being deallocated by the event loop, so this is not a case of a rogue call to dealloc by something else.
You can't rely on reference count as Apple's Framework classes may hold their own references to your objects. The reference count is meaningless for debugging.
Click in the (i) left of the Allocations-Graph
Set "Record reference counts"
You are done ;)

Impact of overreleasing objectsin Cocos2d? - "Message sent to deallocated instance"

I have a cocos2d project. Everything works fine, except when I am replacing a scene.
When replacing the scene, I receive the message "Message sent to deallocated instance" followed by a memory address.
The way my project is structured, most CCNodes are children of the main "Scene" which is replaced. These nodes are also stored in various arrays for iterating among similar objects etc.
I can't imagine how I am over-releasing any of the objects, since adding to arrays should increase the reference count, as should adding to the main scene.
Should I do something about this? What happens if I ignore it? (Turn NSZombies off?) The error pops up when objects are being deallocated, so it should just mean that the object is already release right?
Any suggestions on how I can figure out what I am doing wrong?
You need to fix it, as it will cause crashes once you turn NSZombies off. Somewhere in your code your memory management isn't correct. Xcode can help you find it: run Product -> Analyze (Shift-Command-B).

Programmatically determine if a Cocoa object loaded from nib/xib is available

The setting is the following:
I have a cocoa object in a nib file that is loaded when the NSWindow and view is loaded
The window can be closed
I also access the object programmatically
Now what happens in some situations is that I get a crash, when I try to send a message to the object, but it has been deallocated before (because the window is closed). The crash looks like this:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000017
Crashed Thread: 0
Dispatch queue: com.apple.main-thread
Application Specific Information:
objc_msgSend() selector name: ...
Is there any way to check if the object is available or not? Checking for nil does not work, it is not nil. Probably the control flow is not perfect, and I could rewrite other chunks of the app to make this problem go away, but I think this is a more general problem that I have no solution for, and it boils down to this:
How can I make sure that an object that is loaded from a nib is set to nil on deallocation?
I guess you could use Interfacebuilder to write your Window-class and in its dealloc method you could set the VARIABLE to nil. But you cannot set the object itself to nil. the variable saves a pointer to the object, if its deallocated the pointer points to a place in memory where anything could be.
So if you access said object from another class, you have another variable, so setting the one in your windows-class to nil, wouldnt be useful at all.
The solution is quiet simple, since the window-class sends this object a release message when the window get deallocated, you should retain your object bevor you use it in another class, and then release it when youre done with it.
If you use a property for your object with the retain-attribute dont forget to call the setter with self.object = ... without property it could look sth like this:
so you need to retain your object bevor the window gets closed. maybe in the first viewDidLoad-method that gets called by your app:
...
- (void)viewDidLoad {
otherClassObject.YOUROBJECT = [self.YOUROBJECT retain];
Try deactivating the window setting "Release when close" on Interface Builder.