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 4237944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:50:24+00:00 2026-05-21T02:50:24+00:00

everyone! I have tested this simplest code as following: StorePin.h #import <Foundation/Foundation.h> #import <MAPKIT/mapkit.h>

  • 0

everyone!
I have tested this simplest code as following:

StorePin.h

#import <Foundation/Foundation.h>  
#import <MAPKIT/mapkit.h>   
#import <CORELOCATION/corelocation.h> 


@interface StorePin : NSObject <MKAnnotation> {   

    CLLocationCoordinate2D coordinate;
    NSString *subtitle;   
    NSString *title;   
}   

@property (nonatomic,assign) CLLocationCoordinate2D coordinate;   
@property (nonatomic,retain) NSString *subtitle;   
@property (nonatomic,retain) NSString *title;   

-(id) initWithCoords:(CLLocationCoordinate2D) coords;   

@end

StorePin.m

#import "StorePin.h"


@implementation StorePin

@synthesize coordinate, subtitle, title;   

- (id) initWithCoords:(CLLocationCoordinate2D) coords{   

    self = [super init];   

    if (self != nil) {   

        coordinate = coords;    

    }   

    return self;   

}   

- (void) dealloc
{   
    [title release];   
    [subtitle release];   
    [super dealloc];   
}   

@end 

In my ViewControlller, I made a button to add and remove annotations repeatly.

#import "mapViewTestViewController.h"
#import "StorePin.h"

@implementation mapViewTestViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}


- (IBAction)refresh
{
    [mapView removeAnnotations:mapView.annotations];

    for (int i = 0; i < 101; i ++)
    {
        CLLocationCoordinate2D p1;

        p1.latitude = i/10.0;   
        p1.longitude = i/10.0;  

        StorePin *poi = [[StorePin alloc] initWithCoords:p1]; 
        [mapView addAnnotation:poi];

        [poi release]; 
    }
}


- (void)dealloc
{
    [super dealloc];
}

@end

If I loop less than 100 times to add and remove annotations, all work normally. But if I loop more than 100 times, it will cause memory leak once. I’m nearly crazy to this strange problem. Is this my code’s bug or mkmapview’s bug? Thank you for helping me.

  • 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-21T02:50:25+00:00Added an answer on May 21, 2026 at 2:50 am

    You don’t say what objects have been detected as leaking, but if they are StorePins, then it’s MapKit’s problem — your memory management code for the StorePins you create in the loop is just fine.

    One thing that you do that might be causing MapKit trouble is passing the map view a reference to its own ivar that you want it to modify. It doesn’t seem too likely — if it was really a problem, it would probably cause a crash rather than a leak. However, you might try making a copy, either shallow (as Kai wrote earlier, but absolutely do not follow the advice about using retain counts and calling release in a loop):

    NSArray * annotationsCopy = [NSArray arrayWithArray:mapView.annotations];
    

    or deep:

    NSArray * annotationsDeepCopy = [[[NSArray alloc] initWithArray:mapView.annotations 
                                                          copyItems:YES] 
                                                autorelease];
    

    then pass the copy to removeAnnotations:.

    The second option creates an autoreleased array with a copy of every item in the annotations list so that the map view doesn’t try to remove the same instances that it’s iterating over. Obviously this uses twice the memory; you probably only want to bother with this for bug-hunting.

    If it fixes the leak, great, if not, then there’s probably nothing you can do about it.

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

Sidebar

Related Questions

hello everyone I have this snippet of the code: local helper(f, i, j) =
Now I do have a hw question for everyone...I've been staring at this for
Some details XAMPP 1.7.1 OS tested on vista and xp Hello everyone. I have
Hi everyone I have use a <%= Ajax.ActionLink like this... <%= Ajax.ActionLink(Change Image, ChangeImgWithData,
I have a central git repository that everyone pushes to for testing and integration,
Hi everyone my problem today is I have a couple of buttons that slide
Hello everyone Masters Of Web Delevopment :) I have a piece of PHP script
everyone. I have some labels that I draw them in the xib file, and
Hallo everyone, i have a list of nodes ListNode and i want to draw
everyone seems interested in building IPhone apps today. Do you have to have an

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.