2011-01-21 3 views
0
-(void)determineClosestLocationToUser:(NSArray *)allLocations 
           locationOfUser:(MapLocation *)userLocationIn 
{ 
    // Determine how many objects you have in the array (incase you'e too lazy to count, or changed the 
    // code to enter locations dynamically 
    NSUInteger counter = [allLocations count]; 

    // set this value to the first item in the array, so it has something to compare to 
    closestLocationFound = [allLocations objectAtIndex:0]; 

    // run a for loop to compare each of the locations in the array against the user location to determine 
    // which location is the closest 
    for(NSUInteger i = 0; i < counter; i++) 
    { 
     // setup 2 variables to hold the distances between the user and both locations for comparison 
     CLLocationDistance distance1 = 0; 
     CLLocationDistance distance2 = 0; 

     // create a CLLocation variable using the values from our MapLocation variable 
     // in order to utilize the getDistanceFrom function 
     CLLocation *user = [[CLLocation alloc] initWithLatitude:userLocationIn.coordinate.latitude longitude:userLocationIn.coordinate.longitude]; 
     CLLocation *closest = [[CLLocation alloc] initWithLatitude:closestLocationFound.coordinate.latitude longitude:closestLocationFound.coordinate.longitude]; 

     // create a variable to hold the values stores in the array 
     // (this should be accessed directly from the array, but I'm not sure how to do it yet - CHANGE THIS) 
     MapLocation *tempLoc = [[MapLocation alloc] init]; 
     tempLoc = [allLocations objectAtIndex:i]; 

     // create a CLLocation variable to hold the coordinates for each object in the array 
     // (has to be CLLocation for the getDistance from function to work) 
     CLLocation *check = [[CLLocation alloc] initWithLatitude:tempLoc.coordinate.latitude longitude:tempLoc.coordinate.longitude]; 

     // get the distance from the current closest location 
     distance1 = [user getDistanceFrom:closest]; 
     // now get the distance from the next location in the array 
     distance2 = [user getDistanceFrom:check]; 

     // if the location we just checked is closer than the location we're currently storing 
     if(distance2 < distance1) 
     { 
      // declare that location the closest 
      closestLocationFound = [allLocations objectAtIndex:i]; 
     } 

     // clean up 
     [user release]; 
     [tempLoc release]; 
     [check release]; 
     [closest release]; 
    } 
} 

私はリークのパフォーマンスツールで自分のコードをチェックし、それが私のアプリは唯一のリーク(カテゴリ:MapLocation、eventTypeを:mallocの、サイズ:48、responsibleCaller :(^上記の機能)を有して示して)関数をダブルクリックすると、私はこの行に移動します。ヘルプ

distance1 = [user getDistanceFrom:closest]; 

ご意見やご提案はありますか?前もって感謝します。

答えて

3
MapLocation *tempLoc = [[MapLocation alloc] init]; 

これはその行です。あなたは、その後のalloc /新しいMapLocationをinit'ing、そしてすぐにそれを投げ

tempLoc = [allLocations objectAtIndex:i]; 

この行が実行されると、tempLocの元の値が漏洩しています。その最初の行は無用であり、これらは、それが持っている面白い

MapLocation *tempLoc = [allLocations objectAtIndex:i]; 
+0

に組み合わせる必要があり、いくつかそれ以上のコメント:) – schnaader

+0

@Kevinバラード「これを変更」:私はこれが事実であるとは思いません。たとえば、NSArrayを割り当てて、直後に何か他のものに設定すると、リークは発生しません。営業担当者は、計器が転記されたラインで発生した漏れを捕らえたと述べた。その行は 'MapLocation'とのやりとりがありません。 –

+0

完璧な作業で漏れはありません。 (しかし、私はそれをテストし続けます)迅速な返答をありがとう。私が最初にそれを試してみたとき、文法が間違っていたに違いないと思います。 –