I have upgraded my XCode to versio 3.2.3 to support iOS4 on my iphone project. using the static analyser I checked for memory management problems.
In one of my routines I get the following problem:
I generate a user alert after adding an event to the calendar to give him a status.
This runs fine, but the memory analyser doesn't like how I defined the alert.
I can't see the coding problem, do you? (I indicated the memory analyser hints with "<<<<")
- (IBAction) addToCalendar {
...
UIAlertView *tmpAlert = [UIAlertView alloc]; <<<<Method returns an Objective-C object with a+1 retain count (owning reference)
calData.startDate = iVar.zeitVon;
calData.endDate = iEvent.zeitBis;
calData.title = iVar.title;
calData.calendar = myEventStore.defaultCalendarForNewEvents;
if ([tmpEventStore saveEvent:tmpEvent span:EKSpanThisEvent error:&tmpSaveError]) {
// Show a save success dialog
[tmpAlert initWithTitle:#"Success" <<<<Object released
message:#"entry saved" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
} else {
// Show a save error dialog
[tmpAlert initWithTitle:#"Error"
message:#"entry not saved" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] ;
}
[tmpAlert show]; <<<<Reference counted object is used after its released
[tmpAlert release];
}
thanks
You should never decouple alloc and init. init often changes the object behind the scenes! Try
NSString* foo=[NSString alloc];
NSLog(#"%p %#", foo, [foo class]);
foo=[foo initWithString:#"bar"];
NSLog(#"%p %#", foo, [foo class]);
You'll see something like
2010-07-14 01:00:55.359 a.out[17862:903] 0x10010d080 NSPlaceholderString
2010-07-14 01:00:55.363 a.out[17862:903] 0x100001070 NSCFString
This shows that +[NSString alloc] doesn't really allocate anything; rather, what does the job is initWithString itself. I don't think UIAlertView does this, but you never know.
To recap: never decouple alloc and init. I think the static analyzer just assumes that everyone use [[... alloc] init], so that it got confused by your code. The analyzer should have warned you not to decouple alloc and init.
Related
I have this NSURL like so:
NSURL *url = [NSURL URLWithString:#"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
and it works fine when I am connected to the internet. But if I am not (or if the user has no signal) I would want an UIAlertView to appear. I tried the following code below (with and without the if statement) but my app crashes.
if(!url){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test Message"
message:#"This is a test"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
What Am I doing wrong?
What I mean by crashing, I mean my app opens and does not get the data from the URL. I do not see any errors, I just get brought to a page with this highlighter:
Thread 6
com.app.root.default-priority
- 0 ucol_getVersion
Next to it is a window with this hightlighted
0x17bbd32: movl 204(%ecx), %edx Thread 6: EXC_BAD_ACCESS (code=2 address=0xcc)
What you are doing only checks if the object url is a NULL pointer or not. If you want to check if a user has internet connection, I would recommend trying the Reachability classes, which can be found easily online. This one is very useful for me.
it is recommended to check the Internet availability if you are developing the app with internet connection requirement by the Apple and they are also providing the .h and .m files namely Reachability.h and Reachability.m so please download that files and import to your projects and there is a parameter named "internetConnectionStatus" and by that values use the below alert
if (internetConnectionStatus == NotReachable)
{
UIAlertView *responseAlert = [[UIAlertView alloc] initWithTitle:#"Network Error"
message:#"This application requires network connectivity."
delegate:self
cancelButtonTitle:#"Exit"
otherButtonTitles:#"Continue", nil];
[responseAlert show];
}
else
{ //do other stuff here
}
you can find the reachability files Here and you can use any Reachability files from github
ThnQ
I have implemented the NSStream delegate. I have implemented the same as Witap Application
In that, I have implemented handleEvent delegate
- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode{
switch(eventCode) {
case NSStreamEventOpenCompleted:
{
[tcpServer release];
tcpServer = nil;
break;
}
case NSStreamEventHasBytesAvailable:
{
//done my stuff here
}
case NSStreamEventErrorOccurred:
{
UIAlertView *wifiLostAlert = [[UIAlertView alloc] initWithTitle:#"Wifi
connection error" message:#"" delegate:nil cancelButtonTitle:#"Continue" otherButtonTitles:nil];
[wifiLostAlert show];
[wifiLostAlert release];
wifiLostAlert = nil;
}
}
In my client site, they reported an issue as
"Multiple time when we was either trying to send a message to the
learner or synching devices she got the following message: “Wifi
connection error.
Same happend at 2 different networks and it doesnot recover quickly
even if the network is pretty strong signal."
Unfortunately, I am unable to reproduce this issue in my site and it's working fine in another client site too!!.
Any clue's regarding the issue. Any help on this is appreciated.
Thank you.
I would recommend you supply the client with an updated version that displays more information about what error is actually occurring.
You can get more information about the error using:
NSError* error = [stream streamError];
You can find more information about NSError at NSError Class Reference.
Something like this might work in your case:
NSString* errorMessage = [NSString stringWithFormat:#"%# (Code = %d")",
[error localizedDescription],
[error code]];
Then change your UIAlertView to this:
UIAlertView *wifiLostAlert = [[UIAlertView alloc]
initWithTitle:#"Stream Error"
message:errorMessage
delegate:nil
cancelButtonTitle:#"Continue"
otherButtonTitles:nil];
This won't solve the problem but will give both you and your client more information about the root cause is.
For example, you may find the error is "Connection refused." which would point to a problem not with WiFi signal strength but in the server-side software.
Good luck!
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.
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.
I am trying to clean up my code from memory leaks and I am having problems with the 'release' method.
Here is my code:
NSArray *dict = [[NSArray alloc] initWithContentsOfURL:url];
if (dict == nil) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Cannot retrieve content. Please try again later."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
return;
}
self.schedule = dict;
[dict release];
[url release]; //I receive a runtime error here, "BAD ACCESS"
I don't understand why when I don't get the same problem with the line above [dict release];
Since you didn't post the code showing how URL was created, here's a general rule to follow:
If create the object with a initializer that starts with "init", then you should probably release it. If it's created another way (convenience method), then it's autoreleased. For example:
NSArray *a = [[NSArray alloc]initWithContentsOfURL:url]; // release this later
NSArray *a = [NSArray arrayWithContentsOfURL:url]; // this will be auto released
Basically you just need to look at whether the framework gave you an autoreleased object or not, because you can't release an autoreleased object or you'll (obviously) get a crash.
Take a look at the Memory Management Guide. It should be required reading.
You are responsible for calling release eat time, you call either alloc, copy, or retain.
In this case you called alloc on dict, but (presumably, although it is not shown where url comes from) not on url.
Objective-C allows you to send messages (e.g. 'release') to nil pointers without consequence.
If the pointer is non-nil and points to something bogus (i.e. an object that's been released), you'll get an EXC_BAD_ACCESS exception. Where does the url parameter come from and what is its retain count ([url retainCount]) before you call release?