UIAlertView and releasing object after showing it - objective-c

I am reviewing some source code and noticed this method below. It isn't releasing the message after allocating it. Shouldn't there be a [message release]; after the show?
- (void)service:(TestService*)service didFailWithError:(NSObject *)error
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Service Error"
message:errorMsg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}

If ARC wasn't enabled, then yes, release should have been called after show.

Related

Passing variables inside Appdelegate

When I use a variable inside AppDelegate and then I use it inside a function the variable doesn't change its value!
Any help? I use objective c
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Show alert for push notifications recevied while the
// app is running
NSString *message = [[userInfo objectForKey:#"aps"]
objectForKey:#"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#""
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
How can I get message outside of this function?
You can make a class variable :
NSString *mesage;//You can use this variable anywhere in AppDelegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Show alert for push notifications recevied while the
// app is running
message = [[userInfo objectForKey:#"aps"]
objectForKey:#"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#""
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];

UIAlertView and memory leak

UIAlertView* av = [UIAlertView alloc];
int a = [self somefunc];
if (a == 1)
{
[[av initWithTitle:nil message:#"test msg 1" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
else if (a == 2)
{
[[av initWithTitle:nil message:#"test msg 2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
[av release];
when I run anlyze on this code i am getting the error "refence counted object is used after it is released" at line [av release];
can I know, where av got released, does show function of UIAlertView released av?
strangly the below code doesnt show any error when analyze tool is used
if (a == 1)
{
UIAlertView* av = [[UIAlertView alloc] initWithTitle:nil message:#"test msg 1" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[av show];
[av release];
}
else if (a == 2)
{
UIAlertView* av = [[UIAlertView alloc] initWithTitle:nil message:#"test msg 1" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[av show];
[av release];
}
You must always use the return value of any init call, because an init function can return a different value. Therefore, if you really want to separate alloc and init then you have to do it like this:
UIAlertView *av = [UIAlertView alloc];
// ...
av = [av initWithTitle:...]; // Might change the value of av !!
[av show];
[av release];
The "spurious release" in your code happens here:
[av initWithTitle:...]
because that might (as explained above) release av and return a different object.
In your first code the object "av" is not sure to be initialized, what if the value of a is not 1 0r 2? av will not be initialized so when your release it there will be some problems.
In your second code av's scope became more specific or local to the conditions of if and else. Thats why xcode is sure that av will be initialized and it is safe to relese av.
show function is not releasing the UIAlertView if it is the question here (in the second code).

UIAlertView delegate methods don't response on iOS 5.1

I created an instance of UIAlertView with two buttons and in the interface file of my class(.h) I set the delegate too but still cant get any response when clicking the buttons. Here is my code:
//myClass.h
#interface MainMenu : UIViewController<UIAlertViewDelegate>
-(IBAction)secondAct:(id)sender;
And the implementation
-(IBAction)secondAct:(id)sender
alert = [[UIAlertView alloc] initWithTitle:#"Dear User"
message:#"Your Request Will be Sent To Security"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
[alert autorelease];
}
and the delegate method:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"UIAlertView delegate works");//this line too isnt displayed
NSString *title=[ alertView buttonTitleAtIndex:buttonIndex ];
if ([title isEqualToString:#"OK"]) {
NSLog(#"OK Pressed");
}//i want to create something like this
}
I did all of the code above but still can't take any action. Whichever of the buttons i click, it dissmisses the alert. What is wrong with this code can any one help?
ANSWER
Dear PeterG. commented to change delegete:nil with delegate:self and it works now.
Delegate should probably be set to self. If it generates an error post it here.
Also I do not think you should autorelease the alert ivar. Try to skip that line and see what happens?
Just give the delegate "self".
-(IBAction)secondAct:(id)sender
alert = [[UIAlertView alloc] initWithTitle:#"Dear User"
message:#"Your Request Will be Sent To Security"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
}

Alert View in the Beginning and the End of a void

I have a Void. This void do something really slow, so at the beginning of Void I put an alert, and in the end I put another warning. Like here:
-(void)action {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Start" message:#"Start." delegate:self cancelButtonTitle:#"Ok." otherButtonTitles:nil];
[alerta show];
[alert release];
//Something really slow
UIAlertView *alertEnd = [[UIAlertView alloc] initWithTitle:#"End" message:#"End." delegate:self cancelButtonTitle:#"Ok." otherButtonTitles:nil];
[alertEnd show];
[alertEnd release];
}
But when I run this code, the alerts are only shown at the end of the Void, after all the slow action.
How can I fix this?
First of all: try to avoid using many alerts since it's not humane to you users. Show UIActivityIndicator instead.
If you must, then try this code for showing the first alert:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Start" message:#"Start." delegate:self cancelButtonTitle:#"Ok." otherButtonTitles:nil];
[alert performSelector: #selector(show)
onThread: [NSThread mainThread]
withObject: nil
waitUntilDone: NO];
you will have to separate the two in two methods and then call one using
[self performSelector: afterDelay:]
method

Uialertview and memory management

If I alloc unit a UIAlertview and then show it. Should I release it after the show or should I autorelease it?
This is the common way of showing an alert:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Title:"
message: #"The Message"
delegate: self
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
Release it after the show. It will retain itself until no longer needed.
It is good programming style to release it manually. Autorelease will also do the same thing but at it's own time. So it might release it latter also.