EXC_BAD_ACCESS code 2 on UIAlertView in iOS6 - uialertview

I'm trying to figure out why im getting this crash in my app.
It works perfectly fine in Xcode 4.4 running in the simulator with ios5.1, but when i switch into xcode 4.5 and ios6 I'm getting an EXC_BAD_ACCESS code 2. Here is my code:
- (void) myMethod
{
UIAlertView *alertview = [[[UIAlertView alloc]initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
alertview.tag = 1
[alertview show];
}
this is giving me an EXC_BAD_ACCESS code 2 on the [UIAlertView show] line
any ideas?
thanks!

I've got it.
I have the same problem, in my case it seems that the method is thrown from background now (now in ios7, in ios6 UIAlertView was automatically put into the main-thread as #nodepond says -thanks!-)..
try to assure that the method is shown from main thread:
[alertView performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
Good luck!

It's happened with me, even in 2014.
The problem is want to use an object already released.
What I did wrong:
//class B with UIAletViewDelegate
-(void) showAlert{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle bla bla...];
[alert show];
}
//class A
viewDidLoad{
MyClassB *B = [[B alloc] init];
[B showAlert];
}
What is the right way:
//Class A
#implementation A{
ClassB *B;
}
viewDidLoad{
B = [[B alloc] init];
[B showAlert];
}

Related

EXC_BAD_ACCESS error - sometimes only

my app has a button that checks whether an entered value is correct.
sometimes it causes it to crash, but the strange thing is that it happens at irregular intervals (sometimes on the third iteration, sometimes the tenth, sometimes never).
i get a EXC_BAD_ACCESS error in the debugger. so it seems like something is being released when it shouldn't be. the button calls this function:
- (IBAction)checkValue:(id)sender{
int actualDifference = [firstNumberString intValue] - [secondNumberString intValue];
actualDifferenceAsString = [NSString stringWithFormat:#"%d", actualDifference];
if ([answerTextField.text isEqualToString:actualDifferenceAsString])
{
UIAlertView *correctAlert = [[UIAlertView alloc] initWithTitle:#"matches"
message:#"next value."
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles: nil];
[correctAlert show];
[correctAlert release];
}
else
{
UIAlertView *incorrectAlert = [[UIAlertView alloc]
initWithTitle:#"does not match"
message:#"next value."
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles: nil];
[incorrectAlert show];
[incorrectAlert release];
}
using zombies pointed to the first statement:
int actualDifference = [firstNumberString intValue] - [secondNumberString intValue];
does anyone know what the problem might be?
If zombies are detected in the first line, that means some other part of your program is releasing firstNumberString or secondNumberString. That's where the problem starts, but it only shows up here, when you later try to access those values. Where else do you work with those strings? Do you ever release them?
For overall safety, they should probably be assigned properties, not member variables.
Change it into
NSInteger actualDifference = [firstNumberString intValue] - [secondNumberString intValue]; //change int to NSInteger
NSString *actualDifferenceAsString = [NSString stringWithFormat:#"%d", actualDifference];
if ([answerTextField.text isEqualToString:actualDifferenceAsString])
{
UIAlertView *correctAlert = [[UIAlertView alloc] initWithTitle:#"matches"
message:#"next value."
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles: nil];
[correctAlert show];
[correctAlert release];
}
else
{
UIAlertView *incorrectAlert = [[UIAlertView alloc]
initWithTitle:#"does not match"
message:#"next value."
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles: nil];
[incorrectAlert show];
[incorrectAlert release];
}
you get this code. It worked for me...

[__NSCFSet iName]: unrecognized selector sent to instance

I just do not seem to follow this.
This is my code
-(void)saveClicked:(id)sender{
Item *item=[[Item alloc]init];
item.iName=nameField.text;
if ([appDelegate.list containsObject:item]) {
//currentItem and item are object of class Item
//currentItem was declared in the headerfile and synchronized
currentItem=item;
NSString *msg=[NSString stringWithFormat:#"%# already exists in your Instock list",item.iName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"edit",nil];
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex==0) {
}
else {
//getting the error here
NSLog(#"%#",currentItem.iName);
}
}
ERROR:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet iName]: unrecognized selector sent to instance 0x5e280b0'
I have no clue why this is happening. Help would be appreciated.
Assume you have currentItem as retain property , Try with using below with your saveClicked:
self.currentItem = item;
So, you code should be ...
-(void)saveClicked:(id)sender{
Item *item=[[Item alloc]init];
item.iName=nameField.text;
if ([appDelegate.list containsObject:item]) {
//currentItem and item are object of class Item
//currentItem was declared in the headerfile and synchronized
self.currentItem = item;
NSString *msg=[NSString stringWithFormat:#"%# already exists in your Instock list",item.iName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"edit",nil];
[alert show];
[alert release];
}
[item release];
}
It doesn't make sense to release item as that would mean you've taken ownership only once and you're relinquishing it. So the currentItem will be pointing to a deallocated object. You will have to take ownership by retaining the object and later releasing it once you're done with the object.

iPhone: Using Alerts to Help Debugging

I've been building a rather complex system and there's come the time now where I want more concise debugging. I would like to display the contents of a variable (for this example an NSString called v_string) in a notification window (the kind of window that appear when you receive an SMS text).
Is there an easy way to just call an alert with a variable?
Thanks in Advance,
Dan
NSLog does not do? If not (like if you need to debug an application running on a disconnected device), you can extend the UIAlertView with a category:
#implementation UIAlertView (Logging)
+ (void) log: (id <NSObject>) anObject
{
NSString *message = [anObject description];
UIAlertView *alert = [[self alloc] initWith…];
[alert show];
[alert release];
}
And then in code:
NSString *anInterestingString = …;
[UIAlertView log:anInterestingString];
When you build the string to display in the alert window, simply append your variable's string represenation using stringByAppendingString.
Alert window is cumbersome. Use NSLog instead:
NSLog(#"Variable is: %#", v_string);
And in Xcode's console you will see that text.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"My Debug String" message:v_string delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[message show];
[message release];
I think this way you can see what you want.
But, as zoul said, why not to use NSLog(#"my var: %#", v_string); ?
Hope that it helps.

Am I releasing memory correctly?

I have the following helper object:
LikeHelper* likeHelper = [[LikeHelper alloc]init];
likeHelper.delegate = self;
[likeHelper performLike:self.messageID];
[likeHelper release];likeHelper=nil;
performLike will do some NSURLConnection stuff and then tell the delegate whether or not it was successful.
#pragma mark LikeHelperDelegate Methods
-(void)performLikeFinished:(BOOL)isSuccessful{
if (isSuccessful) {
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"The message has been liked" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
else {
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:#"Error!" message:#"There was a problem liking your message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
Am I releasing the likeHelper in the right place?
If the performLike: method is asynchronous, the likeHelper will propably be released before the performLikeFinished: method is called. You should release the likeHelper in the dealloc: method of the owner object or in the performLikeFinished: implementation in the LikeHelperDelegate to prevent releasing it too soon but if you do that, be aware of JeremyPs comment below!.
If the performLike: method is synchronous, you are doing the right thing but you wouldn't need the delegate to collect the result.
Yes you are, your code is according to the guidelines.
If your code do not work then the problem might be that LikeHelper need to retain self from within -[LikeHelper performLike:].
You should also not retain the LikeHelperDelegate, that might be another cause of confusion or errors.

UIAlertView Pops Up Three Times per Call Instead of Just Once

I am getting odd behavior from an NSAlert in two different parts of my program. The behavior is:
Alert appears and then spontaneously disappears.
Alert reappears and then remains until dismissed by user i.e. normal behavior.
Alert reappears again.
This behavior only occurs the first time the method that displays the alert is called. After that first time, it behaves normally.
Here is the code for the one of the parts in which the behavior occurs:
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
Or if you prefer, with a bit more context:
- (IBAction)locateMe {
NSLog(#"About to check location");
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locMan.distanceFilter = 1609; //1 mile
[locMan startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation * )oldLocation {
if (newLocation.horizontalAccuracy >= 0) {
CLLocation *airportLocation = [[[CLLocation alloc] initWithLatitude:51.500148 longitude:-0.204669] autorelease];
CLLocationDistance delta = [airportLocation getDistanceFrom: newLocation];
long miles = (delta * 0.000621371) + 0.5; //metres to rounded mile
if (miles < 3) {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
[locMan stopUpdatingLocation];
} else {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are not in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
[locMan stopUpdatingLocation];
}
}
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"Error." message:error.code delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locMan release];
locMan = nil;
}
Any ideas? Thanks.
Edit---------
The other place this happens is:
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:#"Unable to download feed from web site (Error code %i )", [parseError code]];
NSLog(#"error parsing XML: %#", errorString);
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
For context the first case is in the AppDelegate and the second in the view controller for the 1st tab view. The second problem occurs every time the xml is reloaded when there is no internet connection. The first one only occurs the first time the function is called.
Edit-----
If I move the alert it works. Unfortunatly this is not where I want it!
- (IBAction)locateMe {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
/*
NSLog(#"About to check location");
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locMan.distanceFilter = 1609; //1 mile
[locMan startUpdatingLocation];*/
}
Update:
I set some NSLog entries and discovered that despite the addition of [locMan stopUpdatingLocation] the didUpdateToLocation function was running multiple times.
I guess the spontaneous disappearance happens because the alert view is called again and the programme clears the first instance to make way for the second automatically.
Any ideas as to why [locMan stopUpdatingLocation] doesn't work would be appreciated but in the mean time I just moved the declaration of the locationAlert out of the function (so it is global), set it in the initial locate me function and use the following the first time it is called:
[locationAlert show];
locationAlert = nil;
That way it works perfectly.
You're not turning off your location manager when you first show the alert. As the location is refined by the device (ie, the accuracy is increased), your callback will be (potentially) called multiple times. You should use [locMan stopUpdatingLocation] after your alert display.
I set some NSLog entries and discovered that despite the addition of [locMan stopUpdatingLocation] the didUpdateToLocation function was running multiple times.
I guess the spontaneous disappearance happens because the alert view is called again and the programme clears the first instance to make way for the second automatically.
Any ideas as to why [locMan stopUpdatingLocation] doesn't work would be appreciated but in the mean time I just moved the declaration of the locationAlert out of the function (so it is global), set it in the initial locate me function and use the following the first time it is called:
[locationAlert show];
locationAlert = nil;
That way it works perfectly.
I think the NSAlert disappearing on its own is the key to solving this.
It's simple to explain why an alert displays unexpectedly i.e. it's just been called unexpectedly. However, it's not so common to programmatically dismiss an alert. Whatever is causing it to disappear is most likely triggering the display again.
To debug I suggest:
(1) Looking in your code for the NSAlert – dismissWithClickedButtonIndex:animated: method and see if somehow you're actually dismissing the alert programmatically.
(2) I believe (someone double-check me on this) that an alert view is added as a subview to whichever base view is currently on screen. It might be that the base view is disappearing for some reason and taking the alert view with it. If the view disappears and then reappears rapidly enough, it might not be obvious when the alert is frontmost. (Edit: see Ed Marty's comment below.)
(3) Since this happens in two separate pieces of the app, compare both to find a common element or structure. That common element might be the cause.
An odd problem.
Edit01: Updated for additional info
If locMan isan instance variable, it should be defined as a property and you should access it every time withself.locMan By accessing it directly, you lose your automatic retention management.
I encountered the same exact issue with the alert dialog appearing momentarily, reappearing, and finally appearing again after being dismissed. I was making a string comparison before deciding to show the alert view:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if([string isEqualToString:#"OK"]) {
NSLog(#"(Settings)Registration Successful");
statusField.text = #"Registration successful!";
[settingsActivity stopAnimating];
}
else {
NSLog(#"(Settings)Registration Failure");
[settingsActivity stopAnimating];
UIAlertView * regFail = [[[UIAlertView alloc] initWithTitle:#"Registration Error!" message:#"Please check your email address and try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[regFail show];
}}
To correct this behavior I simply verified the returned string rather than just showing the alert:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if([string isEqualToString:#"OK"]) {
NSLog(#"(Settings)Registration Successful");
statusField.text = #"Registration successful!";
[settingsActivity stopAnimating];
}
else if([string isEqualToString:#"Error"]) {
NSLog(#"(Settings)Registration Failure");
[settingsActivity stopAnimating];
UIAlertView * regFail = [[[UIAlertView alloc] initWithTitle:#"Registration Error!" message:#"Please check your email address and try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[regFail show];
}
I also got the Same problem while working on Location Manager. Here i checked with Nslog but it is executing multiple times, finally i fount that i am creating multiple objects and using Sharedinstance for same ViewController that contains Location Manger but i am not releasing the object, so at perticular location how many objects if we create that many times the location detects.So while working on LocationManger check handling objects thoroughly to reduce these type of problems.