button click crashes - iphone

I have below code in a IBAction linked to a UIButton to change the background image on Button Click.
UIImage *imageGreen=[UIImage imageNamed:#"bgGreen.png"];
[clickButton setBackgroundImage:imageGreen forState:UIControlStateNormal];
[imageGreen release];
after clicking the button three times it crashes the app in iPhone Simulator 4.0. I am alreading releasing the imageGreen object, what else can i do to prevent this.

No, you should not release imageGreen.
This variable is received with a convenient method, thus will autorelease itself. Calling -release will cause double-release error and crash the program.
Please read the memory management guide.

Related

iPhone SDK: SDWebImageDownloader Crashing

I'm using the SDWebImageDownloader library to download images asynchronously.
The problem i'm having is when I click the back button before the images finish download the app is crashing on the following line in the SDWebImage Class:
if([delegate respondsToSelector:#selector(imageDownloaderDidFinish:)])
This is how i'm using it in my code:
sdDownloader = [[SDWebImageDownloader downloaderWithURL:headerImgURL delegate:self]retain];
What is causing it to crash? I'm retaining it and i'm not releasing it anywhere.
When you say you are retaining "it", what do you mean by "it"? If it's crashing with EXC_BAD_ACCESS when you try to send a message to delegate, it's likely that delegate is a dangling pointer because it's being released prematurely. What is retaining delegate? What is releasing delegate?

When to show Alert for Startup and coming out of background?

In my project I show an Alert to the user to indicate an 'empty list'.
Right now, I show it in AppDelegate>applicationDidBecomeActive.
I'm doing this because I want the alert to show if the list is empty
at app startup and when coming out of the background (iOS 4.2 through 5.x).
EDIT:
I use a method in the AppDelegate, and call it with a slight delay, and I still get this notice.
[self performSelector:#selector(checkForNoMessages) withObject:nil afterDelay:1.0];
However, this causes a "wait_fences" notice in the debugger and I'd prefer not to submit to Apple with this notice.
Where is the proper place to put a popup Alert so that it appears:
1) At App startup
AND
2) When the App is coming out of the background?
Do I need the Alert in more than one place?
I recommend writing a method in your AppDelegate or better in your root view controller which shows the message. Maybe with some arguments, so you can reuse it but that's up to you.
If you are following the MVC architecture ask your model about existing entries and trigger the Alert message if necessary. But encapsulate this behavior in a controller as well.
application:didFinishLaunchingWithOptions: and applicationDidBecomeActive: are the places where you want to delegate this task to your controller.
More about iOS Multitasking: https://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
Edit:
Don't forget that you have to call the methods from the main thread.
And do all startup stuff first.
OK - the problem wasn't where I called the alert, it was because it was in a method. Once I moved the code from a method into applicationDidBecomeActive, all is well.

UIButton causing crashes

This is a very simple issue but I have no idea why it's happening. In my header file, I've declared a UIButton *leftButton. In the viewDidLoad method I do this leftButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
Then in some other method I have:
leftButton.frame=newFrame;
leftButton.tag=i;
[leftButton addTarget:self action:#selector(leftButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:leftButton];
However I keep getting a crash when the above method is called. Sometimes I get a message, other times it just crashes with no message. I can't really replicate any crash messages, now all I'm getting is just a general crash, but it kept crashing from weird issues such as [UIGestureRecognizer setFrame] is causing problems or [WebView setFrame] or something..for some reason it keeps thinking that leftButton is something else than a UIButton, but this is not the case. There is nothing else named leftButton in my entire project. Any ideas?
Your first method is returning an autoreleased object, most likely it has been released by the time your second method is called. Either retain the button in the first method or use a retained property to hold on to it rather than an ivar.
Also, remember to release this in your dealloc method at the end.

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.

iPhone UIButton Multiple Clicks Problem

I have an UIButton, which logs the content that is present in a couple of UITextField's.But when I click on this UIButton thrice, the simulator hangs and the debugger is opened. Can someone tell me why is this happening. It happens only when there is content in UITextFields.
Without seeing your code it's hard to diagnose, but it sorta sounds like you are releasing something you shouldn't be. The first two times the object is still valid, but the third time might make an object's retain count zero, and therefore prematurely deallocate it. Check the IBAction method for your UIButton and make sure you aren't releasing things that you haven't explicitly allocated.