Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3791962
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:32:58+00:00 2026-05-19T12:32:58+00:00

-(void)determineClosestLocationToUser:(NSArray *)allLocations locationOfUser:(MapLocation *)userLocationIn { // Determine how many objects you have in the

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

I checked my code with the Leaks performance tool and it shows my app only has one leak (category:MapLocation , eventType: Malloc, size: 48, responsibleCaller:( ^ the above function) ) When I double click on the function it takes me to this line.

distance1 = [user getDistanceFrom:closest];

Any ideas or suggestions? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:32:58+00:00Added an answer on May 19, 2026 at 12:32 pm
    MapLocation *tempLoc = [[MapLocation alloc] init];
    

    That’s the line. You’re alloc/init’ing a new MapLocation, and then immediately throwing it out

    tempLoc = [allLocations objectAtIndex:i];
    

    When this line executes, the original value of tempLoc is leaked. That first line is useless and these should be combined in to

    MapLocation *tempLoc = [allLocations objectAtIndex:i];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

-(void)LoadOriginalListFromFile { NSMutableArray *temp; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths
void currentfor() { if(current == NULL) { cout << You don't have any members
- (void)viewDidLoad { [super viewDidLoad]; definedNames = [[NSArray alloc] initWithObjects:@Ohio, @Newark, @Steve, @Coffee, nil];
void (int a[]) { a[5] = 3; // this is wrong? } Can I
void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
void some_func(int param = get_default_param_value());
void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
private void activateRecords(long[] stuff) { ... api.activateRecords(Arrays.asList(specIdsToActivate)); } Shouldn't this call to Arrays.asList return
public void test() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); for (int
typedef void (FunctionSet::* Function)(); class MyFunctionSet : public FunctionSet { protected: void addFunctions() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.