2013-10-20 32 views
6

我在我的大項目中使用MKMapView。當我加載了很多註釋(超過700),並且我在MapKit上的這些點之間畫線時,出現xcode錯誤消息「由於內存錯誤而終止」,並且應用程序崩潰。 你可以看到圖像:如何解決由於內存錯誤而終止

enter image description here

我加入一行:

enter image description here

,如果我有小於700註解,它的工作非常好。我認爲它有一些內存問題。我怎樣才能解決這個問題?任何建議。

// 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文件中有

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

的MKMapView委託方法。

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

這就是整個消息嗎?沒有回溯或例外? – nielsbot

+0

是的消息只是這個。應用程序正在等待30秒添加註釋並添加重疊。突然顯示此消息。 –

+0

在樂器下運行您的應用程序並觀看內存增長。如果這是與另一個問題相同的應用程序,而您的問題超過90個,那很可能是問題所在。 – bbum

回答

5

最近 - 並且隨着頻率的增加 - 我已經看到了同樣的錯誤消息(「終止因內存錯誤」)每次我跑在Xcode項目的時間,之後幾乎完全1分鐘運行時,即使開始後我不會觸摸該應用程序。

錯過:

  • 當分析器運行的任何令人驚訝的內存消耗。
  • 當該應用被終止(比的時間長度所花費的出現其他,但花了一段時間來識別)中的任何明顯的圖案。
  • 任何「終止應用程序由於未捕獲的異常」的錯誤信息,或者在控制檯堆棧跟蹤。
  • 拋出任何異常(異常斷點;異常:全部;中斷:拋出)。
  • 任何殭屍對象。

另外,我有只看到應用意外存在,當應用程序在調試中運行 - 沒有隨機跳回到跳板,如果我再跑到離它終止後直設備的應用程序。

我要問一個類似的問題,詳細介紹了所有這些細節,並詢問如何在地球上,我可以解決這個問題。

然後,我有一個德哦時刻,並在控制檯中發現兩個內存警告,即使探查並沒有表現出任何內存問題。

殭屍。當我關閉殭屍對象時,內存警告消失了,應用程序不再自發地終止。

編輯:

10個月後,我發現了另一個情況下會發生這種情況。原來,無限循環while也可以這樣做:

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" 
} 
相關問題