2013-10-20 15 views
6

Tôi đang sử dụng MKMapView trong dự án lớn của mình. khi tôi tải một chú thích rất nhiều (trên 700) và tôi vẽ đường thẳng giữa các điểm trên MapKit, tôi nhận được thông báo lỗi xcode "Chấm dứt do lỗi bộ nhớ" và ứng dụng bị lỗi. bạn có thể xem trên hình ảnh:Cách tôi có thể giải quyết Chấm dứt do Lỗi Bộ nhớ

enter image description here.

tôi thêm dòng như sau:

enter image description here

nếu tôi có ít hơn 700 chú thích, nó làm việc rất tốt. Tôi nghĩ rằng nó có một số vấn đề bộ nhớ. Làm thế nào tôi có thể giải quyết vấn đề này?

// 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]; 
     }); 

tập tin .h có

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

MKMapView Đại biểu Phương pháp.

-(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; 

} 
+0

đó là toàn bộ thư? không có backtrace hoặc trường hợp ngoại lệ? – nielsbot

+0

có tin nhắn là chỉ này. ứng dụng đang chờ 30 giây để thêm chú thích và thêm lớp phủ. đột nhiên thông điệp này đang hiển thị. –

+0

Chạy ứng dụng của bạn trong Ứng dụng và xem bộ nhớ tăng trưởng. Nếu đây là ứng dụng tương tự như câu hỏi khác của bạn, nơi bạn có hơn 90 chủ đề, điều đó có thể dễ dàng là vấn đề. – bbum

Trả lời

5

Gần đây - và với tần số ngày càng tăng - Tôi đã được nhìn thấy cùng một thông điệp báo lỗi ("Kết thúc do lỗi bộ nhớ") mỗi khi tôi chạy một dự án từ XCode, sau gần đúng một phút của thời gian chạy, ngay cả khi Tôi không chạm vào ứng dụng sau khi ứng dụng bắt đầu.

Tôi đã không cái thấy:

  • Bất kỳ tiêu thụ bộ nhớ đáng ngạc nhiên khi chạy với các hồ sơ.
  • Bất kỳ mẫu rõ ràng nào khi ứng dụng bị chấm dứt (ngoài khoảng thời gian cần thiết để xảy ra, nhưng phải mất một lúc để nhận ra).
  • Bất kỳ thông báo lỗi "Chấm dứt ứng dụng do không có ngoại lệ", hoặc ngăn xếp dấu vết trong bảng điều khiển.
  • Bất kỳ trường hợp ngoại lệ nào được ném (điểm ngắt ngoại lệ; ngoại lệ: tất cả; ngắt: khi ném).
  • Bất kỳ vật thể zombie nào.

Ngoài ra, tôi có chỉ thấy ứng dụng tồn tại bất ngờ khi ứng dụng đang được chạy trong debug - nhảy không ngẫu nhiên trở lại Springboard nếu tôi lại chạy ứng dụng từ điện thoại ngay sau khi nó đã được chấm dứt.

Tôi sắp hỏi một câu hỏi tương tự, nêu chi tiết tất cả các chi tiết cụ thể này và hỏi làm thế nào để tôi có thể giải quyết vấn đề.

Sau đó, tôi đã có một thời điểm d'oh, và nhận thấy hai cảnh báo bộ nhớ trong giao diện điều khiển, mặc dù profiler không hiển thị bất kỳ vấn đề bộ nhớ.

Zombies. Khi tôi tắt Zombie Objects, cảnh báo bộ nhớ biến mất và ứng dụng không còn tự động chấm dứt nữa.

Edit:

10 tháng sau đó, tôi thấy một tình huống mà điều này có thể xảy ra.Hóa ra, một vòng lặp vô hạn while cũng có thể làm điều này:

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" 
} 
Các vấn đề liên quan