6

I am using MKMapView in my big project . when I load a lot annotation (over 700) and I draw line between those points on MapKit , I am getting xcode error message "Terminated due to memory error" and app is crashing . you can see on image:

enter image description here.

I am adding line like this :

enter image description here

if I have less than 700 annotation , it's working very well . I am thinking it's have some memory problem . How can I solve this problem ?any advice .

    // adding annodation 
    for (int i=0; i<[fetcher.locations count]; i++)//fetcher.locations is NSMutableArray and inside have  locationInfoClass objects .  locationInfoClass is hold CLLocationCoordinate2D.
        {
           locationInfoClass * loc=[fetcher2.locations objectAtIndex:i];

            CLLocationCoordinate2D coordinate1;
            coordinate1.latitude = loc.lat;
            coordinate1.longitude = loc.lon;

            myAnnotation * ann = [[myAnnotation alloc] initWithCoordinate:annCoordinate           title:@"uniqtitle" subtitle:@"uniqsubtitle"];
           [mapView addAnnotation:ann];


        }
        #pragma mark -
        #pragma mark -mapview overlay 


        CLLocationCoordinate2D *coordinates
        = malloc(sizeof(CLLocationCoordinate2D) * [mapView.annotations count]);

        for (int i=0; i<[mapView.annotations count]; i++) {
            myAnnotation * ann=[mapView.annotations objectAtIndex:i];
            coordinates[i]=ann.coordinate;


        }


        self.routeLine = [MKPolyline polylineWithCoordinates:coordinates count:mapView.annotations.count]; // pinlerin sayısı ne kadarsa o kadar çizgi çiziyor.
        free(coordinates);
        [self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.mapView addOverlay:self.routeLine];
        });

.h file have

@property (nonatomic, retain) MKPolyline *routeLine; //your line
@property (nonatomic, retain) MKPolylineView *routeLineView; //overlay view 

MKMapView Delegate Methods .

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    if(overlay == self.routeLine)
    {
        if(nil == self.routeLineView)
        {
            self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 3;

        }

        return self.routeLineView;
    }

    return nil;
}
/*
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id <MKAnnotation> mp = [annotationView annotation];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
    [mv setRegion:region animated:YES];

}*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

    MKAnnotationView *pinView = nil;
    if(annotation != mapView.userLocation)
    {
        static NSString *defaultPinID = @"ftffggf";
        pinView = (MKAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil )
            pinView = [[MKAnnotationView alloc]
                       initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        //pinView.pinColor = MKPinAnnotationColorGreen;
        pinView.canShowCallout = YES;



        //pinView.animatesDrop = YES;
        UIImage * image=[UIImage imageNamed:@"pin.png"];
        CGSize size=CGSizeMake(50, 63);//set the width and height
        pinView.image = image
    }
    else {
        [self.mapView.userLocation setTitle:@"I am here"];
    }
    pinView.centerOffset = CGPointMake(0,-23);
    return pinView;

}
Erhan Demirci
  • 4,173
  • 4
  • 36
  • 44
  • that's the entire message? no backtrace or exceptions? – nielsbot Oct 20 '13 at 20:25
  • yes message is only this . app is waiting 30 second for add annotation and add overlay . suddenly this message is showing. – Erhan Demirci Oct 20 '13 at 20:34
  • Run your app under Instruments and watch memory growth. If this is the same app as your other question where you have more than 90 threads, that could easily be the issue. – bbum Oct 21 '13 at 16:21
  • Duplicate?: http://stackoverflow.com/questions/19203790/is-it-possible-to-debug-terminated-due-to-memory-error – Andrew Nov 11 '13 at 11:40
  • Related: http://stackoverflow.com/questions/19711184/termination-due-to-memory-pressure-with-known-cause-unknown-solution – Andrew Jan 07 '14 at 04:17

1 Answers1

5

Recently — and with increasing frequency — I have been seeing the same error message ("Terminated due to Memory Error") every time I ran a project from XCode, after almost exactly one minute of runtime, even if I don't touch the app after it started.

I was not seeing:

  • Any surprising memory consumption when run with the profiler.
  • Any obvious patterns in when the app is terminated (other than the length of time it took to occur, but that took a while to recognise).
  • Any "Terminating app due to uncaught exception" error messages, or stack traces in the console.
  • Any exceptions being thrown (exception breakpoint; exception: all; break: on throw).
  • Any zombie objects.

Also, I have only seen the app exist unexpectedly when the app is being run in debug — no randomly jumping back to Springboard if I re-ran the app from the device straight after it was terminated.

I was about to ask a similar question, detailing all these specifics and asking how on earth I could solve the problem.

Then I had a d'oh moment, and noticed two memory warnings in the console, even though the profiler didn't show any memory issues.

Zombies. When I turned off Zombie Objects, the memory warnings disappeared, and the app no longer spontaneously terminated.

Edit:

10 months later, I found another situation where this can happen. Turned out, an infinite while loop can also do this:

while (result==nil) {
    result = [collectionView indexPathForItemAtPoint:testPoint];
    testPoint = nextTestPoint(); // After about 12 seconds, at which point this is several tens of thousands of pixels off the edge of the screen, the app dies from "Memory error"
}
BenRW
  • 443
  • 6
  • 15