ios - 我如何解决因内存错误而终止

标签 ios objective-c xcode mkmapview mapkit

我在我的大项目中使用 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 委托(delegate)方法。

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

}

最佳答案

最近——而且频率越来越高——每次我从 XCode 运行一个项目时,我都会看到相同的错误消息(“因内存错误而终止”),几乎刚好一分钟的运行时间,即使我没有应用程序启动后触摸它。

没有看到:

  • 使用探查器运行时任何意外的内存消耗。
  • 应用程序终止时的任何明显模式(除了它发生的时间长度,但需要一段时间才能识别)。
  • 任何“由于未捕获的异常而终止应用程序”错误消息,或控制台中的堆栈跟踪。
  • 抛出任何异常(异常断点;异常:全部;中断:抛出时)。
  • 任何僵尸对象。

此外,我看到应用程序在调试中运行时意外存在——如果我在终止后立即从设备重新运行应用程序,则不会随机跳回 Springboard .

我正要问一个类似的问题,详细说明所有这些细节,并询问我到底该如何解决这个问题。

然后我有一个 d'oh 时刻,并注意到控制台中有两个内存警告,即使分析器没有显示任何内存问题。

僵尸。当我关闭 Zombie Objects 时,内存警告消失,应用程序不再自发终止。

编辑:

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"
}

关于ios - 我如何解决因内存错误而终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19482396/

相关文章:

ios - 使用 NSURLSessionDataTask 链式请求

ios - 来自 AVCaptureVideoDataOutput 的 CMSampleBuffer 意外地发现 nil

objective-c - 调用 NSFetchedResultsController 的 fetchedObjects 属性是否会导致所有对象出错?

ios - ios如何判断app处于active状态还是inactive状态?

ios - 如何通过底部中心旋转图像?

objective-c - 仅通过模型 Controller (API 包装器)更新模型对象的最佳方法是什么

ios - 升级到 Xcode8 后 React Native RCCTCustomScrollView 损坏

ios - 您将如何编写连续删除按钮的程序?

objective-c - 关于ios的内存管理

ios - 堆栈 View 中的 slider 想要调整大小