iOS4 (UIAlertView) why this code causes memory leaks? - iphone

suppose you create a new iOS app from scratch, with just one single window.
then you put this code in the appDelegate application didFinishLaunching method :
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:#"alert"
message:#"message"
delegate:nil /* same problem with 'delegate:self' */
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[myAlert show];
[myAlert release];
build and run in simulator 4.1, attach instrument, and...
this causes each time a memory leak.
in simulator 3.1.2 on leopard, no problem at all.
Of course, in a real app, the UIalertView is trigerred by a button, but the result is identical.
What is the problem ?
is UIAlertView buggy until iOS4 ?

Don't check for leaks in a simulator. It doesn't have the same memory model so reports leaks when there aren't any.
Test on a real device and if the leak is still there, report it as a bug to Apple :)

Related

Getting memory leaks in my iPhone application project how should I resolve it?

I am getting memory leaks in one of the method which I used in my iOS project. I am not able to find out what is happening as I am new in iOS development.
http://screencast.com/t/y2lOtssY2NjO
You are calling init twice on your alertView.
I think that makes the issue.
Change that like:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:str message:kAlertMessage delegate:self cancelButtonTitle:nil otherButtonTitles:#"Install now", #"Cancel", nil];
Please refer this question : What happens if i call init more than once
The static analyser is pointing out the leak on UIAlertView .Initialize the alertView only once.
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:str.....and so on
You can find via instrument here is tutorial for click here that will tell you or Xcode->product->analyze

UIAlertView Causes Crash in iOS SDK 6.0

I recently submitted an app to App Store that has not been accepted yet. Meanwhile, I downloaded Xcode 4.5 and tested my app in iPhone 6.0 Simulator.
However, when I intend to show an UIAlertView, the app crashes on [myAlertView show] line with EXC_BAD_ACCESS error. It works fine with iPhone 5.1 Simulator.
Here's my code:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"Are you sure?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[myAlertView show];
Is that normal? What am I doing wrong with my code?
Also, do you think I should resend my app to Apple?(Just asking your advice)
Thanks in advance.
Edit: Apple has rejected the app because of iOS 6 crash.
Delete this method and use the other method:
[myAlertView show];
Try this Method:
[myAlertView performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
See this related Question:
UIAlertView shown from background thread and with no delegate creates EXC_BAD_ACCESS
User input and UI calls must come from the main thread. Many of them will work "most" of the time on other threads, but sometimes will crash. They are more likely to crash differently (more or less often) on a device than the simulator, but it's possible Apple made changes that affect that difference in iOS6.
And to your second question, I would answer yes, I would upload an updated binary for review as soon as you fix the issue, especially if you can reproduce this on a device (I strongly encourage you to attempt to reproduce it on a device). Otherwise, you may A) get accepted with a bug your users hate, or B) get rejected and have a an even longer delay and possibly more scrutiny. If you send an updated binary before review finishes it does put you at the "back of the line" AFAIK, but better than the alternatives.

UIAlertView in ARC app

I have iphone sample application with one button. On tap it calls code:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"test"
message:#"test"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
Application has ARC enabled.
The problem is that if I click on OK button in alert, application crashes with EXC_BAD_ADDRESS - probably because of alert is already removed by arc.
What is recommended way to solve this? without adding property to viewcontroller
thanks
My guess is either you haven't implemented the UIAlertViewDelegate methods or that self has gone out of scope.
If you don't care about being alerted when someone dismisses the alert box, change the delegate to nil.
e.g.
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"test"
message:#"test"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
The delegate property of UIAlertView is declared as a weak reference (indicated by the assign keyword). This means ARC will not increment the retain count and you will need to retain a reference to the delegate object (whether it is self or a separate object). This reference should be maintained for the full life of the UIAlertView.
#property(nonatomic,assign) id /*<UIAlertViewDelegate>*/ delegate; // weak reference
I assume that the reason for this is to prevent a loop and failure to release as in the common case the object creating the UIAlertView will retain a reference to it and be it's delegate so closing the loop and preventing release.
This is probably a common pattern for delegates but I hadn't realised until I hit exactly the same issue. I ran in the simulator with zombie detection enabled and it clearly indicated the reference counts and I saw that it was not being retained by the UIAlertView. That was when I checked the header file.
The answer to this question provides some additional information about delegates on system classes and the assign keyword.
iPhone ARC Release Notes - dealloc on system classes delegates?

UIAlertView after completion of NSLog

I am new to iphone..
Actually i wanted to know is it possible to put an UIAlertView after the completion of NSLog as i wanted to check whether the statement really works or not as i cant check NSLog in the iphone.
Please suggest me whether it is possible and how?
NSLog will still work on iPhone..
You can only see it in the console if you have attached the device to your mac..and then use the app.
Anyways.
You can show your AlertView after log like this.( This Nslog is an example..replace with what is your actual log)
NSLog(#" Image Loaded");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Log" message:#"Image Loaded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
[alert release];
If the NSLog statement works, it'll show up in the XCode console. So if you debug the app on your phone via XCode, you should see the NSLog. Or did you have something else on your mind ?

iPhone app crashes while checking internet connectivity

In my iPhone app, I need to detect the internet Connection availability.
So I am referencing some files from "Reachability" project of Apple.
Link is given below:
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
I create a new project and implement the code below in viewWillAppear but the app crashes.
I included the Reachability.h, Reachability.m from Apple's demo project.
I also included SystemConfiguration Framework.
app works fine when Internet is Working. But app Crashes when Internet in not Working.
Even I checked the console but there is no notification or error shown in the console.
Reachability *r = [Reachability reachabilityWithHostName:#"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus == ReachableViaWiFi) || (internetStatus == ReachableViaWWAN))
{
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Internet Connection" message:#"Available" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
}
else
{
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"No Internet Connection" message:#"This app require an internet connection via WiFi or cellular network to work." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
}
What could be the reason for crash?
What should I do?
Thanks!!
You're over-releasing the alerts. First, you do autorelease and then additionally release, which is too much. Just remove the two [myAlert release]; and it should work.
Here it may be the case that your code does not work on simulator because of time out. But try running it on device. Also try debugging the code as #greg rightly said. For that credit should go to #greg. Hope this helps. Let me know if it works.
Your problem is elsewhere in your code, as the code you provided along with copying Reachability.[mh] into a fresh Xcode project seems to work without crashing. When your app crashes, it must have some sort of information logged in the console. If there truly is nothing, set a breakpoint in your "startup" methods (viewWillLoad, viewDidLoad, AppDelegate stuff and step through line-by-line until you catch the problem.