Segmented Control Problem - iphone

My program will crash if I press the segmented control buttons one after another. For example, if i press the first one, then second, and third, it will crash. Any ideas for what could be causing this. I also release it and everything, really stumped here. Any ideas.
Thanks
- (IBAction) segmentedControlIndexChanged {
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
[self mirror];
break;
case 1:
[self exact];
break;
case 2:
[self round];
break;
}
}
I'm getting a EXC_BAD_ACESS
if that means anything, cause i'm not sure what that means
error on line 2179 of "/SourceCache/gdb/gdb-1510/src/gdb/macosx/macosx-nat-inferior.c" in function "macosx_kill_inferior_safe": (os/kern) failure (0x5x)

EXEC_BAD_ACCESS means you are attempting to call a method on a deallocated instance. The tricky thing about fixing these errors is that it can happen well after the instance was released, so what you're doing at the time isn't necessarily what is causing the error.
Fortunately, there is a tool to help you. NSZombieEnabled
Go down to your executables folder in XCode, and right click on the app and click Get Info.
Go to the Arguments Tab, and click the plus button below variables to be set in the environment.
Call the new variable NSZombieEnabled and set it's value to YES
When you enable this, any instance that gets deallocated gets replaced by a Zombie object, and your console should display an object and "message sent to deallocated instance" which should help you track down your problem.

Related

Unable to find Zombie in Instruments for device

Unable to find Zombie in Instruments for device .
I am able to find Zombie in Instruments for Simulator but not able to find for device , my app only run on device due to addition of third party api.
How can i trace cause of crash due to "message sent to deallocated instance "
I just want to find exact instance(or line of code) which is causing this crash.
Although, when XCode returns "Message sent to deallocated instance" error message, it normally tells the Object as well which is sending that error.
Anyway, you can use the below to find the exact line which is causing you the error:
Use Exception BreakPoint for All Exceptions in XCode.
To Add an Exception Breakpoint:
1. Go to BreakPoint Navigator
2. At the bottom there is a plus symbol. Click it.
3. You will get two options: Add Exception Breakpoint... and Add Symbolic BreakPoint...
Choose Exception Breakpoint.
Some reasons for zombies crash:
1. Control delegate response late after the control instance is cleared.
2. Instance used inside the thread try to modify the instance after its get cleared.
So handle the delegate properly
Make control delegates to nil while removing view controller like this:
- (void)viewDidDisappear:(BOOL)animated
{
[self.mapView setDelegate:nil];
[_webView setDelegate:nil];
}

EXC_BAD_ACCESS when adding and removing a view from different views

I have created a game with a in game shop view and view controller.
The shop can be accessed in then menu (ViewController.m) and from the Game Over screen (GameViewController.m).
My problem is that if I have displayed the shop once in the menu, and then play a game and access the shop in the game over screen and try to buy something, the app crashes trowing a EXC_BAD_ACCESS error without much info. (Breaking at
[[SKPaymentQueue defaultQueue] addPayment:lPayment];
line in the ButtonPressed action in my ShopViewController when trying to buy a IAP.
My view are set up like this:
Menuview -> Ladderview -> Gameview -> ShopView
and
Menuview -> Shopview
Hope you can help me pinpoint the error,
EDIT -----------
It seems that I can reproduce the error from the menu -> Shopview without using the game view. I can do this by pressing a "buy button", pressing cancel, navigate back to the menu, go back to the shop, and repeat. On the 3-4th attempt it crashes at the same line. Here is the entire button pressed method:
- (void)buyButtonPressed:(UIButton *)pButton {
NSInteger lTag = [pButton tag];
//////NSLog(#"Button tag: %i"), lTag;
Reachability *lReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus lCurrentNetworkStatus = [lReachability currentReachabilityStatus];
if (lCurrentNetworkStatus != NotReachable) {
if ([SKPaymentQueue canMakePayments]) {
SKPayment *lPayment = [SKPayment paymentWithProduct:[mPriceArray objectAtIndex:lTag]];
[[SKPaymentQueue defaultQueue] addPayment:lPayment];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
} else {
[self showAlertViewWithText:#"Purchases are disabled. Please check your settings for General -> Restrictions -> In-App Purchases and try again." andTitle:#"Warning"];
}
} else {
[self showAlertViewWithText:#"No network connection!" andTitle:#"Warning"];
}
}
So it might seem as the lPayment is being deallocated. I even tried to set
mProductIds = nil;
mPriceArray = nil;
when I remove the shop view, trying to force it to allocate it again when I reload the shop, but without any luck.
Thanks
Your issue is a dangling pointer. EXC_BAD_ACCESS is the CPU moaning that you are addressing non-existent memory or memory which is outside of your access rights area. The cause is a lack of retainment of an object which causes early deallocation and then is overwritten. At which time (which may be delayed), the pointer will point to garbage whose dereference (class examination) causes an EXC_BAD_ACCESS to be thrown. This error canNot be caught using #try. There is an assumption here that the stack itself is corrupt causing continuation to be impossible (although such is most likely not the case), which will throw the debugger for a spin, whose current state output is already lacking in many areas. It is like uncontrollable anarchy when the CPU resets important registers and performs a long jump.
consider Automatic Reference Counting. In you are already there, consider that delegate-like properties are not retained by the host object. Any property which could logically contain self will not retain any value stored in it. ARC will not help you there.
in your case: defaultQueue is probably good. lPayment has probably been deallocated.
Try to trace the problem at first enabling NSZombie . In case of EXC_BAD_Access Problem some time it(NSZombie ) becomes more useful to trace deallocated object than simple guessing where the problem is.
It is hard to tell from the information provided, but it could be the following: Your statement
SKPayment *lPayment = [SKPayment paymentWithProduct:[mPriceArray objectAtIndex:lTag]];
instantiates an SKPayment object, and hands it over to the current autorelease pool. If this pool does not exist (this might be the case if the code runs in a separate thread for which no autorelease pool has been set up explicitly), the object is released immediately again, and your statement
[[SKPaymentQueue defaultQueue] addPayment:lPayment];
accesses invalid memory.

app crashes without any log messages

In my App I have an UIViewController, that pushed by another ViewController's navigation controller. It contains some views, buttons, scrollViews and accelerometer support. When I tapping "back" button of navigationController, app crashes without any log message, except this one:
"warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J2)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
(gdb)"
debugger links me to this line in main.m:
int retVal = UIApplicationMain(argc, argv, nil, nil);
with "EXEC_BAD_ACCESS"
what does this mean?
EDIT:
all of you are right.
problem was in accelerometer. I setted delegate ([UIAccelerometer sharedAccelerometer].delegate = self;) and didn't remove it. that's why there was no line in my code for debugger to link to. I just added this:
- (void)viewWillDisappear:(BOOL)animated {
[UIAccelerometer sharedAccelerometer].delegate = nil;
}
and problem gone. So, If you are using any device functions, be careful with delegates.
Did you set Zombies to enabled? That will enable you to track if you access an already released object, and that tells you which object it is.
If you are using XCode 4, you can enable zombies in Project -> Edit Scheme -> Diagnostics by checking the "Enable Zombies" checkbox.
Also make sure you have "Break on Exception" set on - in XCode 4 go to the Breakpoint View, press the Plus sign in the lower left corner, and choose "Add Exception Breakpoint ..." for all exceptions. Then XCode will break at the point where the exception occurs, and you will see more than just UIApplicationMain as the location.
This means you have tried to read/write a block of memory you have no permission to. Maybe you're trying to use an object you haven't allocated/initialized. Check your code, debug it and inspect variables to find a solution.
I guess you had a memory warning and you app deallocated some datas tha are not there anymore when you go back.
Put some breakpoint into didReceiveMemoryWarning, dealloc, viewDidUnload and viewDidLoad to see what happens in your previous controller.
EXC_ BAD_ ACCESS is an exception (EXCeption_ BAD_ ACCESS).
If you set a breakpoint on objc_exception_throw (sign + on the lower left corner of debug tab), you'll get those.
You might want to look at NSZombieEnabled (http://www.cocoadev.com/index.pl?NSZombieEnabled), as you're probably trying to access a dealloc'd object.

application crashes on calling popViewController : error: alertView:didDismissWithButtonIndex:

A description of the problem is as follows:
I have a view, say, view A. To enter certain data, I have an alert,with a text field inside it, which pops up. Once the user enters data into the text field, i have an alertView:didDismissWithButtonIndex: function as follows :
- (void)alertView:(UIAlertView *)alertView:didDismissWithButtonIndex:(NSInteger)buttonIndex {
[ amountEntered resignFirstResponder]; //dismiss keyboard
if (buttonIndex == 1) { //OK clicked, do something
if(lblShowTypedText.text)
data.investmentAmount = lblShowTypedText.text ;
[myTable reloadData];
}
}
Then I have a submit button on my View A, which when clicked pops back to the previous view. Here is where my app crashes. There is no message in the console, however after many runs, I got one message like this:
* -[NSCFType alertView:didDismissWithButtonIndex:]: unrecognized selector sent to instance 0x3c4dce0
2010-06-24 15:33:22.970 BankingAppln[2895:207] CoreAnimation: ignoring exception: * -[NSCFType alertView:didDismissWithButtonIndex:]: unrecognized selector sent to instance 0x3c4dce0
Thus i have narrowed down the problem to the alertView:didDismissWithButtonIndex: function. If I do not call the alert, but directly pop back to the previous view, everything is fine.
I must be doing something wrong in my alertView:didDismissWithButtonIndex: function.
Pls help!!
A few things to check:
You set the delegate of the AlertView to the right class (View A)?
Your class (View A) implements the UIAlertViewDelegate protocol.
Probably not, but you never know: You're classname is not equal to a name in apple's private api (don't laugh, happened to me a week ago, costed me 2 hours to figure out)?
EDIT:
Another thing to check:
Your delegate method has the right return type (I think it's "void" in that case)?
Do you really have this method, alertView:didDismissWithButtonIndex:, in your class? and post the code when you call it as well
You need to post where you call the method..but from the error message you gave, the problem is you are calling your method incorrectly.
if it is a method you defined yourself with the implementation above use
[self alertView:myAlertView didDismissWithButtonIndex:myIndex];
also, in your declaration, you have a semicolon after the parameter alertView and you just need a space.
I faced a similar problem and it turns out that with Automatic Reference Counting in place, I needed to keep a reference to the popup around as a property so that it would not be reference collected. That much was fine but I got overzealous and started doing stuff like popup = nil; explicitly and that got me into trouble because some of the delegate methods for the popup were called after I had nil'ed out the reference that I was holding onto and now this popup was not around anymore and the framework crashed due to this little fact.
[__NSCFString alertView:didDismissWithButtonIndex:]: unrecognized selector sent to instance 0x9117c0
So I decided to simply keep allocating a new popup when it was needed and not explicitly nil'ing out the older references. This fixed the issue for me.

NSZombieEnabled hides EXC_BAD_ACCESS error entirely

So I have a subclass of a UIView that starts causing EXC_BAD_ACCESS errors when I go through a specific set of conditions (run on iPad instead of iPhone or simulator, first login only). It throws the exception when the UIView subclass gets autoreleased from the pool (i.e. the pool is releasing, not when I'm calling [view autorelease], during the last line, where I have [super dealloc]. I heard about using NSZombieEnabled, so I tossed that on to see if I could get any more information about it, but now it hides the error completely!
Does anyone know a bit more about this type of situation? I thought NSZombie would start spewing stuff into my console like before, but I'm hoping that the nonexistance of errors would tell me some sort of information as well.
- (void)dealloc
{
[loadingLabel release];
[indicatorView release];
[super dealloc];
}
Edit: Ok, so I sorta solved the underlying problem:
One of my properties was:
#property (nonatomic,retain) NSString * title;
However, the code for that property is as follows (loadingLabel is a UILabel):
- (void)setTitle:(NSString *)title
{
loadingLabel.text = title;
[loadingLabel sizeToFit];
[self setNeedsLayout];
}
- (NSString *)title
{
return loadingLabel.text;
}
I don't actually retain anything, but rather only do a UILabel.text, which is a copy property. So I changed my own title property to reflect this, and the error has disappeared.
However, I still don't really know how or why this error popped up in the first place, and why it only appears on the iPad platform (not the iphone, or even the ipad simulator).
So I have a subclass of a UIView that starts causing EXC_BAD_ACCESS errors when I go through a specific set of conditions (run on iPad instead of iPhone or simulator, first login only). It throws the exception when the UIView subclass gets autoreleased from the pool (i.e. the pool is releasing, not when I'm calling [view autorelease], during the last line, where I have [super dealloc]. I heard about using NSZombieEnabled, so I tossed that on to see if I could get any more information about it, but now it hides the error completely!
You get EXC_BAD_ACCESS because you tried to work with a dead object.
NSZombieEnabled makes objects not die. Since the object never died, your app didn't crash. Instead, you'll get a message in the Console log (the Debugger Console when running under Xcode), telling you what you did wrong and suggesting a breakpoint to set.
To be more specific, the NSZombie message will tell you what class of object you sent a message to, and what message you sent it. If you set the breakpoint and run your app with the debugger active, the debugger will interrupt (“break”) your app at that point, so that you can look around and see what sent the zombie object the message.
I don't actually retain anything, but rather only [assign to] a UILabel.text, which is a copy property.
Thus, the string presumably died off for not being owned by anything. With NSZombieEnabled and the above technique, you can confirm the theory that it was the string that was dying off prematurely.
A better and easier way, though, is to use Instruments's Zombies template. Instead of a message appearing in Xcode's Debugger Console, a flag in will appear Instruments's timeline with the information. That flag will have a go-to-iTunes-Store (➲) button you can click for more information about the problem.
Could you clarify - do you mean that you throw an EXC_BAD_ACCESS exception or you (on purpose) cause the system to throw an EXC_BAD_ACCESS exception? How do you do this? Please show some code. NSZombie will spew to the console if you try to call an instance method on it. Are you doing that?
NSZombie should produce output to the console in the same location a crash would have occurred.
Have you tried also running Instruments with the ObjectAlloc tool, and enabling both retain count and zombie detection in there (you need it both in the executable and configure the ObjectAlloc instrument, click on the "i" next to ObjectAlloc). When a message is sent to a zombie that instrument will flag you with a special popup saying a zombie was detected.
Remember the iPad SDK is still beta, maybe you should report this but to apple if you can confirm this to be a bug. Unfortunately their bugtracker is horrible from my experience.
The behavior of NSZombie is to never decrease an objects retain cound if release is called. However it keeps track of the would-be retain count and logs acess to the object after it would have been deleted on the console. It puts the Object adress alongside the error message, together with Instruments you can easily locate the Object at that adress and determine where it has been alocated and who retained/released it.