如何解决因内存错误而终止的问题 [英] How I can solve Terminated due to Memory Error

查看:113
本文介绍了如何解决因内存错误而终止的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的大项目中使用MKMapView。当我加载很多注释(超过700)并在MapKit上的这些点之间画线时,我收到xcode错误消息由于内存错误而终止并且应用程序崩溃。您可以在图片上看到

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:

我正在添加这样的行:

如果我的注释少于700,它的效果非常好。我在想它有一些记忆问题。我怎么能解决这个问题?任何建议。

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文件

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

MKMapView委托方法。

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;

}


推荐答案

最近 - 并且随着频率的增加 - 每次运行XCode项目后,我都会看到相同的错误消息(由于内存错误而终止),即使我在运行时差不多一分钟后,即使我没有触及应用程序开始。

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.

看到:


  • 使用分析器运行时任何令人惊讶的内存消耗。

  • 应用程序终止时的任何明显模式(除了发生的时间长度,但需要一段时间才能识别)。

  • 任何因未捕获的异常而终止应用程序错误消息或控制台中的堆栈跟踪。

  • 抛出任何异常(异常断点) ; exception:all; break:on throw)。

  • 任何僵尸对象。

  • 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.

,我只有 看到应用程序在调试中运行时意外存在应用程序 - 没有r如果我在终止后直接从设备上重新运行应用程序,那么就会跳回Springboard。

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。当我关闭Zombie Objects时,内存警告消失了,应用程序不再自发终止。

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

编辑:

10个月后,我发现了另一种可能发生这种情况的情况。原来,无限循环也可以这样做:

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

这篇关于如何解决因内存错误而终止的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆