Question on when and where to release an object - iphone

I have several places where I need to display an alert and handle the dismiss operation in the same way, namely take them to the same view controller. Instead of duplicating the alert specific code in all those places, I created a separate class like below:
AlertUtility.h:
#interface AlertUtility : NSObject {
}
- (void) displayAlert;
#end
AlertUtility.m:
#implementation AlertUtility {
- (void) displayAlert {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:... message:...
delegate:self cancelButtonTitle:... otherButtonTitles:nil] autorelease];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// Create another view controller and display its views
[self release] // Release current object because I'm not releasing it where I create it
}
}
Where I need to use this alert (i.e. in MyViewController), I have the following:
MyViewController.m:
AlertUtility *utility = [[AlertUtility alloc] init];
[utility displayAlert];
As you can see, I am not releasing the utility object here (which I should since I own it), but rather in the didDismissWithButtonIndex method of the AlertUtility class. I am clearly missing something here.
I've tried autoreleasing the utility object but then by the time the didDismissWithButtonIndex method is 'called' on the utility object, that object was already released (due to the autorelease).
I've tried making the displayAlert method static (and calling it with [AlertUtility displayAlert];), but then the didDismissWithButtonIndex is never called.
Should I just release the current object from within the didDismissWithButtonIndex method, like I do now, or is there a way to release it in MyViewController instead (without creating a AlertUtility property for the current class)?
Thanks!
EDIT
Should I use the Singleton pattern instead maybe?

Calling [self release] is a big no-no because no object has information about what other object refer to it. The retention system is intended to make sure that if ObjA needs ObjB, that ObjB will remain in memory until ObjA no longer needs it. If ObjB releases itself, the entire system breaks down.
In this case, the UIAlert itself retains the AlertUtility object as it's delegate so you can release the AlertUtility as soon as your done with it just as you would a UIAlert.
So,this is fine:
AlertUtility *utility = [[AlertUtility alloc] init];
[utility displayAlert];
[utility release]; // released here, but still alive
It's not a problem if the AlertUtility object is still alive past the release. A release doesn't necessarily kill an object. It merely says that the object sending the release message has no further need of the released object. Each retaining object just has the responsibility to balance all retains (including implicit ones like init) with a release. Past that the retaining object has no interest and no responsibility for whether the system keeps released object alive or not.
After all, any single object may be retained by multiple retaining objects each with no knowledge of the other. In this case the AlertUtility is retained once by the ViewController with init. Then it is retained again by the UIAlert as a delegate. (Edit: UIAlert assigns it delegate)When ViewController releases the AlertUtility object, it will remain alive as long as the UIAlert needs it and vice versa.
Since the UIAlert object is retained by the UIWindow when it is displayed this means that the AlertUtility object will remain alive until after the UIAlert is dismissed. (Edit: this is valid only for the view, not the AlertUtility delegate)
You would not normally have a UIAlert delegate whose existence is solely dependent on that of the UIAlertitself. Usually, the UIAlert delegate is the view controller that evokes the alert. That way, you don't have to worry about the delegate dying before it completes the task associated with the alert.

You can use this instead:
void displayAlert(NSString *title, NSString *message)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
Or just:
#define MY_ALERT(TITLE, MESSAGE) \
[[[[UIAlertView alloc] initWithTitle:TITLE \
message:MESSAGE \
delegate:nil \
cancelButtonTitle:#"OK" \
otherButtonTitles:nil] autorelease] show]
And then write:
MY_ALERT(nil, #"Press, OK!");

Your pattern works, although I agree releasing it "elsewhere" is ugly and I try to avoid this. You could make the following approach:
// AlertUtility.h:
#interface AlertUtility : NSObject {
}
- (void) displayAlert;
#end
// AlertUtility.m:
#implementation AlertUtility {
- (void) displayAlert {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:... message:...
delegate:self cancelButtonTitle:... otherButtonTitles:nil] autorelease];
[alert show];
[self retain]; // explicitly retain this (self) object
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// Create another view controller and display its views
[self release] // Release current object because you retained it earlier here
}
}
And later:
// MyViewController.m:
AlertUtility *utility = [[AlertUtility alloc] init];
[utility displayAlert];
[utility release]; // released here, but still alive
Edit: removed the release on the alert, as it was autoreleased.

Related

Crash when UIAlertView is clicked

When I click the UIAletView, I receive the following error.
alertView:clickedButtonAtIndex:]: message sent to deallocated instance 0x84c7010
This is the code I have used.
UIAlertView *testAlert = [[ UIAlertView alloc]initWithTitle:messageTitle message:messageBody delegate:self cancelButtonTitle:messageClose otherButtonTitles:messageTryAgain, nil];
testAlert.tag = 2;
[testAlert show];
[testAlert release];
And I have the delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
}
When I click the UIAlertView , even before the control reaches the delegate method, the app crashes. What could be the reason. What am I doing wrong?
This is "one hack of a solution".
Hopefully it helps you understand that your delegate is the memory issue. The delegete (in this case self) is deallocated somehow before the testAlert is dismissed
// retain self to avoid crash you were experiencing earlier
UIAlertView *testAlert = [[ UIAlertView alloc]initWithTitle:messageTitle message:messageBody delegate:[self retain] cancelButtonTitle:messageClose otherButtonTitles:messageTryAgain, nil];
testAlert.tag = 2;
[testAlert show];
[testAlert release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// release self because you've gotten past the crash
[self release];
}
This is in no way an elegant solution and should encourage you to debug your app further to find out why self is being deallocated prematurely
Just wondering, could you show us your .h file?
If I had to hazard a guess, you've forgotten to set your class to respond to UIAlertViews as a delegate
You might be missing something like this:
#interface MyClass : UIViewController <UIAlertViewDelegate>
If ARC enable UIAlertView Object retain and no need to release, it's automatically release your object.

iPhone - How to handle errors at runtime

When writing code, there are many situations that must be treated as runtime errors : an alloc/init returns nil, a resource is not found, a [someClass canDoThis] returns NO for an absolutely-needed feature where YES would be the natural answer, ...
For all these situations, I have written an exitWithMessage routine (that displays an alert box), and each class has a kill method that frees allocated memory.
So... When in an init method, you have these kind of exceptions, I supposed you could do :
[self kill];
[OneClass exitWithFatalErrorMessage];
return nil;
- (void) exitWithFatalErrorMessage:(NSString*)message
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedStringFromTable(#"Error" #"ErrorMessages", #"") message:message delegate:self cancelButtonTitle:NSLocalizedStringFromTable(#"Understood", #"ErrorMessages", #"") otherButtonTitles: nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// stop the normal running of the app, there is a situation that would prevent it
}
- (void)kill
{
self.member = nil;
self.member2 = nil;
...
}
But this does not work... My alert is not displayed (the exitWithMessage works fine when used anywhere else than into an init method.
How would you handle those cases ? Is this piece of coding a fine way to do ?
If yes, why does my alert do not display (I'm into a view controller initWithCoder method for the example) ?
Are you actually calling the exitWithFatalErrorMessage method, because in your code you call exitWithMessage instead, try changing it to this:
[OneClass exitWithFatalErrorMessage:#"Message"];

UIAlertView exits EXC_BAD_ACCESS error

I have such error: when I click navigationbar.backItemButton I'm showing UIAlertView with two buttons. When I press on any of them application terminates just with EXC_BAD_ACCESS. Method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex don't called. How can I solve it? Thanx!
//h - file
#interface DetailsTableViewController : UITableViewController <UITextFieldDelegate, UIAlertViewDelegate>
//m - file
- (void)viewWillDisappear:(BOOL)animated
{
//if changes unsaved - alert reask window
if (isDirty)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Save changes?"
message:#"Press YES if you want to save changes before exit, NO - other case."
delegate: self
cancelButtonTitle: #"NO"
otherButtonTitles: #"YES", nil];
[message show];
[message autorelease];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex: buttonIndex];
if([title isEqualToString: #"YES"])
{
[self saveBtnUserClick];
}
}
I think the problem is that after you tapped back button your current controller is removed from navigation stack and deallocated, so when alert tries to call its delegate methods it calls them on deallocated object which results in EXC_BAD_ACCESS error. To workaround the problem I see 2 obvious options (although there may be better solutions):
Extra retain your controller somewhere (in previous controller may be), but you need to find way to release it when you're done.
Create your custom button instead of standard "back" and just show alert when it tapped. Then in alert's delegate method pop your current controller from navigation stack.
Try Changing delegate to nil instead of self. It fixed my issue.
Is your view controller implementing the UIAlertViewDelegate? If not, add in you interface declaration before the { starts.
Also try NSLogging inside the clickedButtonAtIndex method and print the buttonIndex values and see the console.
Edit: Reading your post again, I guess you indeed have missed the UIAlertViewDelegate in your interface declaration.
Probably [message autorelease];
is you mistake use
[message release];
Because you have used [[UIAlertView alloc] init.....]; there for you should release the memory.
autorelease is something will work with the structure which memory is compiler dependent or you have not given the memory manually.
Enjoy.
"Try Changing delegate to nil instead of self. It fixed my issue." worked for me. Thanx

Click on UIAlertView crashes app if view is dismissed

A UIAlertView is displayed if an error occurs. But in the meantime the view on which the UIAlertView were called has been dismissed (and therefore released). If the user clicks on OK the app crashes because a message to a released instance is sent. This will cause your app crashing:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"test" message:#"test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
alertView = nil;
[self.navigationController popViewControllerAnimated:YES];
I thought the UIAlertView is an independent unit. But it seems it isn't. Is there a way how I could avoid the app crashing (except not dismissing the view)?
The delegate is called when the UIAlertView is dismissed, so in your case:
delegate:self
Delegates are not retained, like an object added to an array, or a subview would be. So in your case, when you call:
[self.navigationController popViewControllerAnimated:YES];
self is most likely being released, and when the the user dismisses the alert, self is called, but has been dealloc'd so it no longer exists.
An easy way to check this is to put a logger statement, like NSLog(#"I'm gone"); in self's dealloc method, if it's ran, then you know your self isn't around anymore, and any messages sent to it will cause a crash.
Make the UIAlertView a retained property of your view controller so that you can refer to it in your dealloc, then set the alert view's delegate to nil when the view controller is deallocated.
Be sure to properly release the retained alert view once it's been dismissed and on dealloc.
For instance:
#interface MyViewController : UIViewController <UIAlertViewDelegate> {
UIAlertView *alertView;
}
#property (nonatomic, retain) UIAlertView *alertView;
#implementation MyViewController
#synthesize alertView;
- (void)showAlert {
if (alertView) {
// if for some reason the code can trigger more alertviews before
// the user has dismissed prior alerts, make sure we only let one of
// them actually keep us as a delegate just to keep things simple
// and manageable. if you really need to be able to issue multiple
// alert views and be able to handle their delegate callbacks,
// you'll have to keep them in an array!
[alertView setDelegate:nil];
self.alertView = nil;
}
self.alertView = [[[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Something went wrong"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Retry",nil] autorelease];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertViewParam didDismissWithButtonIndex:(NSInteger)buttonIndex {
self.alertView = nil; // release it
// do something...
}
- (void)dealloc {
[alertView setDelegate:nil]; // this prevents the crash in the event that the alertview is still showing.
self.alertView = nil; // release it
[super dealloc];
}
The downside here is that you will not be able to handle the alert view's callback when the user dismisses it. However, since your controller is already gone/released, presumably you don't need to. If you do, you have to set the alert view's delegate to something that will persist.
If the UIAlertView object is to be usable from anywhere in the app, not just on the current view, then retain it inside something that is available from anywhere in the app, either some persistant root view controller under the entire possible view stack, or the app delegate.
Added:
This top level object can also retain the alert view's delegate until after it's done being needed (after alert view dismissal).
(People might wonder I am late by years in answering this question,but it might help someone else)
I guess your problem lies some where in popping the view controller,you are displaying the alert view and at the same time trying to navigate the user back to a view.I would recommend you to follow a hierarchal approach here i.e.:
First of all declare your alert view as a global object,i.e.
#property(nonatomic,retain) UIAlertView *sampleAlert;
Now write your alert view display code where ever desired,say for instance:
-(IBAction)buttonClicked:(id)sender
{
self.sampleAlert = [[UIAlertView alloc] initWithTitle:#"test" message:#"test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[sampleAlert show];
[sampleAlert release];
}
Finally try to navigate the user to the desired view when the "Ok" button is pressed,i.e. you need to make use of alertView didDismissWithButtonIndex method,i.e.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(alertView == sampleAlert)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
Please note that if you have alert view with multiple buttons,you also need to check for button index for distinguishing actions,i.e. check using
if(alertView == sampleAlert && buttonIndex == 0)
{
//Do your stuff
}
else
{
//Do something else
}
This will definitely avoid application crash,thanks :)
Easier way that worked for me is to hold all the alert views in a Array and when the parent view is deallocated enumerate alertViews array and assign the delegate to nil. This will ensure that the touch event is ignored and app will function.
// ARC world
#property (strong, nonatomic) NSMutableArray *alertViews;
- (void)dealloc
{
[self.alertViews makeObjectsPerformSelector:#selector(setDelegate:) withObject:nil];
}
Make sure you are implementing the UIAlertViewDelegate protocol.
If you don't care about when the alert is dismissed just init with delegate as nil.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"test" message:#"test" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];

iPhone memory management question and object livecycle

Here is the code I have:
Phone SDK undestanding cocoa object live cycle:
- (void) DismissWelcomeMessage: (UIAlertView *) view
{
[view dismissWithClickedButtonIndex:0 animated:YES];
}
- (void) ShowWelcomeMessage
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Blah" message:#"Blah Blah" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[self performSelector:#selector (DismissWelcomeMessage:) withObject: alert afterDelay: WELCOME_MESSAGE_DELAY];
[alert release];
}
ShowWelcomeMessage is called first.
Why DissmissWelcomeMessage works fine and does not crash even though alert object is released?
Is because Dismiss function uses copy of the object passed on stack as a parameter when function? But even then would not it be just a copy of the pointer pointed to the now deallocated object?
Or [alert release] just decriment reference counting and does not really do the same as
delete in C++?
performSelector retains the object, thus your release doesn't cause its retain count to go to zero.
See NSObject docs
This method retains the receiver and the anArgument parameter until after the selector is performed.
It's possible that performSelector is retaining the object passed in, which is why it's still valid when DismissWelcomeMessage is called.