“EXC_BAD_ACCESS” when switch too fast between an tableView and a mapView

天涯浪子 提交于 2019-12-20 06:10:04

问题


I have a tableView with an button to push a mapView. The push and back actions generally work fine. If I switch quickly between these two views, "EXC_BAD_ACCESS" error will appear.

MapViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;    

UIButton *btnL = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40.0, 40.0)];
[btnL setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[btnL addTarget:self.navigationController  action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchDown];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:btnL] autorelease];
[btnL release];

self.whereAmIAnnotation = [[[WhereAmIAnnotation alloc] init] autorelease];

if (!self.mapView || !self.whereAmIAnnotation) {
    NSLog(@"mapview : %@", self.mapView);
    NSLog(@"whereAmIAnnotation : %@",self.whereAmIAnnotation);
 // will never enter in to here
}

[self.mapView addAnnotation:self.whereAmIAnnotation];

}

If I comment [self.mapView addAnnotation:self.whereAmIAnnotation]; , there is no "EXC_BAD_ACCESS" anymore.

Any answers and comments will be appreciated. Thanks in advance!

Edit 2

main.m

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
 // "EXC_BAD_ACCESS" error message shows here
}
}

Edit 3:

the whereAmIAnnotation is declared here:

MapViewController.m

@interface AddrCoordinateAdjustMapViewController()

@property (retain) WhereAmIAnnotation *whereAmIAnnotation;

@end
@implementation MapViewController
@synthesize whereAmIAnnotation;

Edit 4:

Error message is as following:

2012-07-30 15:56:19.735 myApp[13584:707] *** -[MapViewController respondsToSelector:]: message sent to deallocated instance 0x10195e80

It usually crashes when i switch back to the tableView while the annotationView is dropping.


回答1:


Have you removed yourself as delegate of the mapview?

self.mapview.delegate = nil;

try removing it at viewWilldisappear or whenever you consider necessary as you might need it later if you have pushed a view controller and will return to the view later.

In a nutshell, remove it when your view cannot respond to the delegate which is why you got the EXC_BAD_ACCSS. it sent a message to your view but it was already released from memory.




回答2:


Do you release and set to nil all your IBOutlets in the viewDidUnload? So at least you should do:

- (void)viewDidUnload {
   self.mapView = nil;
   [super viewDidUnload];
}

Generally in ViewDidUnload you should release and set to nil, any view objects that are created from a Nib file or allocated in your ViewDidLoad method




回答3:


I would suggest to check whether self.whereAmIAnnotation isnt already released by at the time you add it to mapView. That might be one reason for the BAD_ACCESS you receive.




回答4:


I had similar problem and my solution was to remove all overlays from the map before popping controller from UINavigationController. I was using OpenStreetMap.



来源:https://stackoverflow.com/questions/10893808/exc-bad-access-when-switch-too-fast-between-an-tableview-and-a-mapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!