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

  • SEARCH
  • Home
  • 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 6754087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:14:19+00:00 2026-05-26T13:14:19+00:00

I’ve got a method that takes in the annotations (custom PostLocationAnnotation class) to be

  • 0

I’ve got a method that takes in the annotations (custom PostLocationAnnotation class) to be displayed on a map view and clusters close ones together, outputting an array of MKAnnotation of PostLocationAnnotations and LocationGroupAnnotations (the clusters, which each contain some PostLocationAnnotations). Here’s how I call the function (from within an ‘updateAnnotations’ method, called when the viewport of the map changes):

[annotationsToAdd addObjectsFromArray:[ffMapView annotations]];        
[ffMapView addAnnotations:[self clusterAnnotations:annotationsToAdd WithEpsilon:20.0f andMinPts:4]];

annotationsToAdd is initially populated by the annotations that have been retrieved from the server that have not already been added to the map. Therefore I am passing the full list of the annotations that should be put on the map into the clusterAnnotations method. Here is the body of the method:

- (NSArray *)clusterAnnotations:(NSArray *)annotations WithEpsilon:(float)eps andMinPts:(int)minPts
{
    NSMutableSet *D = [[NSMutableSet alloc] initWithCapacity:[annotations count]];
    NSMutableArray *C = [[NSMutableArray alloc] init];

    for (id <MKAnnotation> annotation in annotations)
    {
        if ([annotation isKindOfClass:[PostLocationAnnotation class]])
        {
             NSMutableDictionary *dictEntry = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                              annotation, @"point",
                                              [NSNumber numberWithBool:NO], @"visited",
                                              [NSNumber numberWithBool:NO], @"noise",
                                              [NSNumber numberWithBool:NO], @"clustered", nil];

            [D addObject:dictEntry];

            [dictEntry release];
        } else if ([annotation isKindOfClass:[LocationGroupAnnotation class]])
        {
            for (PostLocationAnnotation *location in [(LocationGroupAnnotation *)annotation locations])
            {
                NSMutableDictionary *dictEntry = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                  location, @"point",
                                                  [NSNumber numberWithBool:NO], @"visited",
                                                  [NSNumber numberWithBool:NO], @"noise",
                                                  [NSNumber numberWithBool:NO], @"clustered", nil];

                [D addObject:dictEntry];

                [dictEntry release];
            }
        }

    }

    for (NSMutableDictionary *P in D)
    {
        if ([P objectForKey:@"visited"] == [NSNumber numberWithBool:NO])
        {
             [P setValue:[NSNumber numberWithBool:YES] forKey:@"visited"];

             NSMutableSet *N = [[NSMutableSet alloc] initWithSet:[self regionQueryForPoint:P andEpsilon:eps fromList:D]];

             if ([N count] < minPts)
             {
                 [P setValue:[NSNumber numberWithBool:YES] forKey:@"noise"];
             } else {
                 LocationGroupAnnotation *newCluster = [[LocationGroupAnnotation alloc] initWithLocations:nil];
                 [C addObject:newCluster];
                 [self expandDbscanClusterWithPoint:P andRegion:N andCluster:newCluster andEpsilon:eps andMinPts:minPts fromList:D];

                 [newCluster release];
            }

            [N release];

        }
    }


    NSMutableArray *annotationsToAdd = [[[NSMutableArray alloc] initWithCapacity:[annotations count]] autorelease];

    for (NSMutableDictionary *P in D)
    {
        if ([P objectForKey:@"clustered"] == [NSNumber numberWithBool:NO])
        {
            [annotationsToAdd addObject:[P objectForKey:@"point"]];
        }
    }

    for (LocationGroupAnnotation *cluster in C)
    {
        [cluster updateCenterCoordinate];
    }

    [annotationsToAdd addObjectsFromArray:(NSArray *)C];

    [D release];
    [C release];

    return (NSArray *)annotationsToAdd;
}

When I run this I get a zombie message, and I have found that removing [D release] fixes the zombie but causes a leak. Looking at Instruments I can see that the memory address is first Malloc’d in clusterAnnotations, then retained and released a couple of times, then retained a large number of times by regionQueryForPoint (reaching a peak of 47 references), then released twice by clusterAnnotations, then released by [NSAutoreleasePool drain] until the refcount reaches -1 and I get the zombie message error. Here is the code for regionQueryForPoint:

- (NSSet *)regionQueryForPoint:(NSMutableDictionary *)P andEpsilon:(float)eps fromList:(NSMutableSet *)D
{
    NSMutableSet *N = [[[NSMutableSet alloc] init] autorelease];

    for (NSMutableDictionary *dictEntry in D)
    {
        if ((dictEntry != P) &&
            ([[dictEntry objectForKey:@"point"] isKindOfClass:[PostLocationAnnotation class]]))
        {
            CGPoint p1 = [ffMapView convertCoordinate:[[P objectForKey:@"point"] coordinate] toPointToView:self.view];
            CGPoint p2 = [ffMapView convertCoordinate:[[dictEntry objectForKey:@"point"] coordinate] toPointToView:self.view];

            float dX = p1.x - p2.x;
            float dY = p1.y - p2.y;

            if (sqrt(pow(dX,2)+pow(dY,2)) < eps)
            {
                [N addObject:dictEntry];
            }
        }
    }
    return (NSSet *)N;
}

The large number of retains appear to happen when regionQueryForPoint is called from the expandDbScanClusterWithPoint method, so I’ve included that here for completeness:

- (void)expandDbscanClusterWithPoint:(NSMutableDictionary *)P andRegion:(NSMutableSet *)N
                      andCluster:(LocationGroupAnnotation *)cluster
                      andEpsilon:(float)eps
                       andMinPts:(int)minPts
                        fromList:(NSMutableSet *)D
{

    [cluster addAnnotation:(PostLocationAnnotation *)[P objectForKey:@"point"]];
    [P setValue:[NSNumber numberWithBool:YES] forKey:@"clustered"];

    BOOL finished = NO;

    while (!finished)
    {
        finished = YES;

        for (NSMutableDictionary *nextP in N)
        {
            if ([nextP objectForKey:@"visited"] == [NSNumber numberWithBool:NO])
            {
                [nextP setValue:[NSNumber numberWithBool:YES] forKey:@"visited"];

                NSSet *nextN = [self regionQueryForPoint:nextP andEpsilon:eps fromList:D];

                if ([nextN count] >= minPts)
                {
                    [N unionSet:nextN];
                    finished = NO;
                    break;
                }
            }

            if ([nextP objectForKey:@"clustered"] == [NSNumber numberWithBool:NO])
            {
                [cluster addAnnotation:[nextP objectForKey:@"point"]];
                [nextP setValue:[NSNumber numberWithBool:YES] forKey:@"clustered"];
            }

        }
    }

}

I’ve been dissecting this for ages, counting references, watching pointers and everything but I just can’t work out how to safely release this D set. Can anyone see anything I’m not seeing?

  • 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-26T13:14:20+00:00Added an answer on May 26, 2026 at 1:14 pm

    You seem to be over-releasing dictEntry with [dictEntry release];. When using dictionaryWithObjectsAndKeys, you’re getting an autoreleased object back. So releasing it again will decrease the retain count.

    EDIT: If you’re unsure how it works and when you’re actually retaining objects, you might want to have a look at the memory management docs:

    You create an object using a method whose name begins with “alloc”,
    “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or
    mutableCopy).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
i got an object with contents of html markup in it, for example: string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.