帮助泄漏功能

时间:2011-01-21 23:19:44

标签: iphone memory-leaks

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

我使用Leaks性能工具检查了我的代码,它显示我的应用只有一个泄漏(类别:MapLocation,eventType:Malloc,大小:48,responsibleCaller :( ^上面的函数))当我双击该函数时我需要这条线。

distance1 = [user getDistanceFrom:closest];

有任何想法或建议吗?提前谢谢。

1 个答案:

答案 0 :(得分:3)

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

那就是这条线。你分配/初始化一个新的MapLocation,然后立即抛出它

tempLoc = [allLocations objectAtIndex:i];

执行此行时,tempLoc的原始值会泄露。第一行是没用的,这些应该合并到

MapLocation *tempLoc = [allLocations objectAtIndex:i];