0

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.

lu yuan
  • 7,207
  • 9
  • 44
  • 78

4 Answers4

3

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.

Dan1one
  • 1,288
  • 8
  • 13
  • Thank you very much. It really hits the point! I should set the delegate of mapview to nil while leaving the mapview. – lu yuan Jul 30 '12 at 08:16
1

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

iTukker
  • 2,083
  • 2
  • 16
  • 16
0

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.

mooonli
  • 2,355
  • 4
  • 23
  • 32
0

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

ArturOlszak
  • 2,653
  • 2
  • 21
  • 20