Can't get subtitle of Annotation to work -> crash


#1

My program works fine if I don’t add the subtitle to annotation to incorporate the date. It crashes after I type and enter text for Title of annotation. Crash point indicated below with asteriks.

Break is displayed in main:

int retVal = UIApplicationMain(argc, argv, nil, nil); <- Thread 1: Program received signal: “EXC_BAD_ACCESS”.

I’m new, and I’m stuck. Would greatly appreciate help.

Thanks,
Bob

//*********************************************WhereamiAppDelegate.h ************************************************

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "MapPoint.h"

@interface WhereamiAppDelegate : NSObject <UIApplicationDelegate,CLLocationManagerDelegate, MKMapViewDelegate> 
{
    UIWindow *window;
    CLLocationManager *locationManager;
    
    IBOutlet MKMapView *mapView;
    IBOutlet UIActivityIndicatorView *activityIndicator;
    IBOutlet UITextField *locationTitleField;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

-(void)findLocation;
-(void)foundLocation;

@end

//********************************************WhereamiAppDelegate.m *********************************************************

#import "WhereamiAppDelegate.h"


@implementation WhereamiAppDelegate


@synthesize window=_window;

-(void)findLocation
{
    [locationManager startUpdatingLocation];
    [activityIndicator startAnimating];
    [locationTitleField setHidden:YES];
}

-(void)foundLocation
{
    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation];
}

-(BOOL)textFieldShouldReturn: (UITextField*)tf
{
    [self findLocation];
    [tf resignFirstResponder];
    return YES;
}

-(void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id <MKAnnotation> mp = [annotationView annotation];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 250, 250);
    [mv setRegion:region animated:YES];
}

-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"New location: %@", newLocation);
    //Howmay seconds ago was this new location created?
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
    //CLLocationManager will return the last found location of the 
    //device first, you don't want that data in this case.
    //If this location was made more than 3 minutes ago, ignore it.
    if (t < -180)
    {
        //This is cached data, you don't want it, keep looking 
        return;
    }
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    NSString *dateString = [formatter stringFromDate:date];
    MapPoint *mp = [[MapPoint alloc] initWithCoordinate: [newLocation coordinate] title: [locationTitleField text] subtitle:dateString];
    NSLog(@"%@", dateString); // ******************************** crashes after this line ****************************
    [mapView addAnnotation:mp];  
    [mp release];
    [date release];
    [formatter release];
    [dateString release];
    
    [self foundLocation];  // ********** foundLocation call
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"Could not find location: %@", error);
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Create location manager object -
    locationManager = [[CLLocationManager alloc]init];
    
    //Make this instance of WhereamiAppDelegate the delegate
    //it will send its messages to our WhereamiAppDelegate
    [locationManager setDelegate:self];
    
    //Want all results from the location manager
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    
    //Want it to be as accurate as possible
    //regardless of how much time/power
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    
    //Tell our manager to start looking for its location immediately
    //[locationManager startUpdatingLocation];
    [mapView setShowsUserLocation:YES];
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

@end
//***************************************************MapPoint.h*********************************
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MapPoint : NSObject <MKAnnotation>
{
    NSString *title;
    CLLocationCoordinate2D coordinate;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

-(id)initWithCoordinate: (CLLocationCoordinate2D)c title: (NSString*)t subtitle: (NSString*) st;

@end

//********************************************************** MapPoint.m **********************************

#import "MapPoint.h"

@implementation MapPoint
@synthesize coordinate, title, subtitle;

-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)st
{
    [super init];
    coordinate = c;
    [self setTitle:t];
    NSLog(@"%@", title);
    [self setSubtitle:st];
    NSLog(@"%@",subtitle);
    return self;
}

-(void)dealloc
{ 
    [title release];
    [subtitle release];
    [super dealloc];
}
@end

//***************************************** main.m ******************************
#import <UIKit/UIKit.h>

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

#2

Figured it out. No need for [date release] after [NSDate date];
This caused the crash.

Bob