How can I solve termination due to a memory error

I am using MKMapView in my large project. when I upload a lot of annotations (over 700) and I draw a line between these points on MapKit, I get the xcode error message “Terminated due to a memory error”, and the application crashes. you can see in the image:

enter image description here .

I add the line as follows:

enter image description here

if I have less than 700 annotations, it works very well. I think he has a 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]; }); 

The .h file has

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

MKMapView delegation 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; } 
+6
ios objective-c xcode mapkit mkmapview
Oct. 20 '13 at 20:20
source share
1 answer

Recently - and with an increase in frequency - I saw the same error message ("Terminated due to a memory error") every time I started a project from Xcode, after almost exactly one minute of runtime, even if I don’t touch application after its launch.

I did not see:

  • Any unexpected memory consumption when starting with the profiler.
  • Any obvious patterns in case of termination of the application (except for how long it took, but it took some time to find out).
  • Any error message "Application termination due to an uncaught exception" or a stack trace in the console.
  • Exception throws (exception: exception: all; break: on throw).
  • Any zombie objects.

In addition, I saw that the application unexpectedly appears when the application starts in debug mode - without accidentally switching to Springboard if I restarted the application from the device immediately after its completion.

I was going to ask a similar question, detailing all these features and asking how on earth I can solve the problem.

Then I had a moment and noticed two warnings about memory on the console, although the profiler did not detect any memory problems.

Zombies When I disabled Zombie objects, the memory warnings disappeared and the application no longer spontaneously terminated.

Edit:

After 10 months, I discovered a different situation when this could happen. It turned out that an infinite while could 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" } 
+5
May 13 '14 at 13:19
source share



All Articles