问题:程序接收信号:“EXC_BAD_ACCESS”。虽然构建和运行很好

时间:2020-11-29 21:20:15

i'm NEW TO XCODE, working with mapkit and annotations, howver after building and running fine, the application crashes on load. I ran the debugger and;

我是XCODE的新手,使用mapkit和注释,在构建和运行良好之后,应用程序在加载时崩溃。我运行调试器并且;

Stopped at Breakpoint 1 'mapView:viewForAnnotation: - Line 951'

停在断点1'mapView:viewForAnnotation: - 第951行'

continues after a few hit counts,

在几次命中后继续,

Program received signal: "EXC_BAD_ACCESS".

程序收到信号:“EXC_BAD_ACCESS”。

I've read countless solutions something to do with alloc or releasing or something but have no idea where the problem is in my code.

我已经阅读了无数的解决方案,与alloc或release或其他东西有关,但不知道我的代码中的问题在哪里。

EDIT: PROBLEM SOLVED.

编辑:问题已解决。

4 个解决方案

#1


2  

Not a direct answer to your question, but i highly recommend you use a switch statement to replace the long "else if" code. Makes your code look much cleaner and your compiler can do optimizations.

不是你的问题的直接答案,但我强烈建议你使用switch语句来替换长“else if”代码。使您的代码看起来更清晰,您的编译器可以进行优化。

http://en.wikipedia.org/wiki/Switch_statement

Edit; After looking at the other comments (from Eiko), I have to agree that in this case you even want to remove this whole piece of code and replace it with some collection. The code contains too much duplications.

编辑;看完其他评论(来自Eiko)之后,我不得不同意在这种情况下你甚至想要删除这整段代码并用一些集合替换它。代码包含太多重复。

#2


3  

If there is a crash, there is a backtrace. Post it.

如果发生崩溃,则会有回溯。发表它。

#3


1  

Seriously too much code to go through.

严重的代码要经过太多。

Here is how you can figure these errors.

以下是如何计算这些错误的方法。

a) Enable "Stop on Objective-C exceptions" in Run menu of XCode. This will get you pretty close to where the error occurred. Just look at the stack and you will be able to figure.

a)在XCode的“运行”菜单中启用“停止Objective-C异常”。这将使您非常接近错误发生的位置。只要看一下堆栈,你就能算出来。

b) If that does not help, then try the NSZombie route although I have never had a need to use it. http://www.cocoadev.com/index.pl?DebuggingAutorelease . I mostly figure by just reviewing the code.

b)如果这没有帮助,那么尝试NSZombie路线,虽然我从来没有需要使用它。 http://www.cocoadev.com/index.pl?DebuggingAutorelease。我主要是通过查看代码来计算。

Hope this helps.

希望这可以帮助。

#4


1  

mapView:viewForAnnotation could be dramatically shorter, without using plists or collections. A reuse identifier is useful for dequeueing annotations of a particular type or annotations that perhaps have expensive-but-identical setup work. You're initializing every one of your annotations the same way, so they can all use the same reuse identifier without issue. The function could thus be written:

mapView:viewForAnnotation可以大大缩短,而不使用plist或集合。重用标识符可用于使特定类型或注释的注释出列,这些注释可能具有昂贵但相同的设置工作。您正以相同的方式初始化每个注释,因此它们都可以使用相同的重用标识符而不会出现问题。因此可以编写该函数:

- (MainViewAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{   
    if(annotation == mapView.userLocation) return nil;

    NSString* identifier = @"City";
    MainViewAnnotationView *newAnnotationView = (MainViewAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if(nil == newAnnotationView)
    {
        newAnnotationView = [[[MainViewAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease];
    }

    [newAnnotationView setEnabled:YES];
    [newAnnotationView setCanShowCallout:YES];

    return newAnnotationView;
}

#1


2  

Not a direct answer to your question, but i highly recommend you use a switch statement to replace the long "else if" code. Makes your code look much cleaner and your compiler can do optimizations.

不是你的问题的直接答案,但我强烈建议你使用switch语句来替换长“else if”代码。使您的代码看起来更清晰,您的编译器可以进行优化。

http://en.wikipedia.org/wiki/Switch_statement

Edit; After looking at the other comments (from Eiko), I have to agree that in this case you even want to remove this whole piece of code and replace it with some collection. The code contains too much duplications.

编辑;看完其他评论(来自Eiko)之后,我不得不同意在这种情况下你甚至想要删除这整段代码并用一些集合替换它。代码包含太多重复。

#2


3  

If there is a crash, there is a backtrace. Post it.

如果发生崩溃,则会有回溯。发表它。

#3


1  

Seriously too much code to go through.

严重的代码要经过太多。

Here is how you can figure these errors.

以下是如何计算这些错误的方法。

a) Enable "Stop on Objective-C exceptions" in Run menu of XCode. This will get you pretty close to where the error occurred. Just look at the stack and you will be able to figure.

a)在XCode的“运行”菜单中启用“停止Objective-C异常”。这将使您非常接近错误发生的位置。只要看一下堆栈,你就能算出来。

b) If that does not help, then try the NSZombie route although I have never had a need to use it. http://www.cocoadev.com/index.pl?DebuggingAutorelease . I mostly figure by just reviewing the code.

b)如果这没有帮助,那么尝试NSZombie路线,虽然我从来没有需要使用它。 http://www.cocoadev.com/index.pl?DebuggingAutorelease。我主要是通过查看代码来计算。

Hope this helps.

希望这可以帮助。

#4


1  

mapView:viewForAnnotation could be dramatically shorter, without using plists or collections. A reuse identifier is useful for dequeueing annotations of a particular type or annotations that perhaps have expensive-but-identical setup work. You're initializing every one of your annotations the same way, so they can all use the same reuse identifier without issue. The function could thus be written:

mapView:viewForAnnotation可以大大缩短,而不使用plist或集合。重用标识符可用于使特定类型或注释的注释出列,这些注释可能具有昂贵但相同的设置工作。您正以相同的方式初始化每个注释,因此它们都可以使用相同的重用标识符而不会出现问题。因此可以编写该函数:

- (MainViewAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{   
    if(annotation == mapView.userLocation) return nil;

    NSString* identifier = @"City";
    MainViewAnnotationView *newAnnotationView = (MainViewAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if(nil == newAnnotationView)
    {
        newAnnotationView = [[[MainViewAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease];
    }

    [newAnnotationView setEnabled:YES];
    [newAnnotationView setCanShowCallout:YES];

    return newAnnotationView;
}