Manage memory for challenge exercise


#1

Hi, here is my code for chp14 exercise, can anyone have a look and help me to check the memory management. Thanks all for your feed back.

//
//  MapPoint.h
//  Whereami
//
//  Created by Justin Liu on 27/01/11.
//  Copyright 2011 Dropped Pin. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MapPoint : NSObject <NSCoding, MKAnnotation>
{
	NSString *title;
	CLLocation *location;
}
@property (nonatomic, retain) CLLocation *location;
@property (nonatomic, copy) NSString *title;
- (id)init;
- (CLLocationCoordinate2D)coordinate;

@end
//
//  MapPoint.m
//  Whereami
//
//  Created by Justin Liu on 27/01/11.
//  Copyright 2011 Dropped Pin. All rights reserved.
//

#import "MapPoint.h"

@implementation MapPoint
@synthesize location, title;

- (void)encodeWithCoder:(NSCoder *)encoder
{
	[encoder encodeObject:title forKey:@"title"];
	[encoder encodeObject:location forKey:@"location"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
	[super init];
	[self setTitle:[decoder decodeObjectForKey:@"title"]];
	[self setLocation:[decoder decodeObjectForKey:@"location"]];
	return self;
}

- (CLLocationCoordinate2D)coordinate;
{
	return [location coordinate];
}

- (id)init
{
	self = [super init];
	return self;
}

- (void)dealloc
{
	[title release];
	[location release];
	[super dealloc];
}
@end
//
//  WhereamiAppDelegate.h
//  Whereami
//
//  Created by Justin Liu on 27/01/11.
//  Copyright 2011 Dropped Pin. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

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

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

- (void)findLocation;
- (void)foundLocation;
- (IBAction)changeMapType: (id)sender;

- (void)archiveMapPoints;
- (NSString *)mapPointsPath;

@end

Here I archive the MapPoint object, one thing I found is that this didFoundLocation been called 6
times every run, so there will always be 6 objects been archived, any one get idea why it is 6?

//
//  WhereamiAppDelegate.m
//  Whereami
//
//  Created by Justin Liu on 27/01/11.
//  Copyright 2011 Dropped Pin. All rights reserved.
//

#import "WhereamiAppDelegate.h"
#import "MapPoint.h"

@implementation WhereamiAppDelegate
@synthesize window, mapoints;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application 
	didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
	mapoints = [[NSMutableArray alloc] init];
	
	locationManager = [[CLLocationManager alloc] init];
	
	[locationManager setDelegate:self];

	[locationManager setDistanceFilter:kCLDistanceFilterNone];

	[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
	
	NSString *mpPath = [self mapPointsPath];
	
	// Unarchive it into an array
	NSMutableArray *mps = 
	[NSKeyedUnarchiver unarchiveObjectWithFile:mpPath];
    
	if (!mps) {
		mps = [NSMutableArray array];		
	}
	else {
		int count = [mps count];
		[self setMapoints:mps];
		[mps release];
		
               // Here I retrieve the last archived MapPoint object and display it.
                MapPoint *mp = [mapoints objectAtIndex:count - 1];		
		[mapView addAnnotation:mp];	
	}
	[mapView setShowsUserLocation:YES];
        [self.window makeKeyAndVisible];
    
        return YES;
}

- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
		   fromLocation:(CLLocation *)oldLocation
{
	NSLog(@"location ---- %@", newLocation);
	
	NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
	
	if (t < -180){
		// This is cached data, you don't want it, keep looking
		return;
	}
	
	MapPoint *mp = [[MapPoint alloc] init];
	[mp setTitle:[locationTitleField text]];
	[mp setLocation:newLocation];
	
	[mapView addAnnotation:mp];	
	
	[self foundLocation];
	
        // Here I archive the MapPoint object, one thing I found is that this didFoundLocation been called 6
        // times every run, so there will always be 6 objects been archived, any one get idea why it is 6?
	[mapoints addObject:mp];
	[mp release];
}

- (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];
}

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

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

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

- (NSString *)mapPointsPath
{
	return pathInDocumentDirectory(@"MapPoint.data");
}

- (void)archiveMapPoints
{
	// Get full path of possession archive
	NSString *maPointsPath = [self mapPointsPath];
	
	// Get the possession list
	NSMutableArray *mps = mapoints;
	
	// Archive possession list to file
	[NSKeyedArchiver archiveRootObject:mps 
								toFile:maPointsPath];
}

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
    	[self archiveMapPoints];
}

- (void)dealloc 
{
	[mapoints release];
        [window release];
	[activityIndicator release];
	[locationTitleField release];
	[mapView release];
	[locationManager release];
	[segmentedControlMapType release];
       [super dealloc];
}

@end