我知道EXC_BAD_ACCESS是难以确定的事情之一,我知道僵尸模式可以追踪解除分配的对象,但我仍然遇到这个问题。
我有一个自定义MKAnnotation
,我在地图上使用标准的AnnotationView。当我点击地图上的别针时,我崩溃了。如果我放大所以只显示1个引脚,它就可以工作。如果我缩小并点击一个新的引脚,它会崩溃。
令我疯狂的是,这个(据我所知)昨天100%工作,我不记得改变任何代码(不可否认,我已经24小时了,所以...它可能我不记得做了它。)
有一次,我看到一个引脚在崩溃前给出了标题“com.apple.SOMETHING”。听起来像过度释放给我,但标题存储在注释中,我想。
Zombie调试给出:
*** - [CFString length]:发送到已解除分配的实例0x5b7b250的消息。
我的地图视图或注释类中没有使用字符串长度。 我确实有一张没有分配标题的照片,但是如果这是照片的引脚在发生崩溃时在屏幕上被点击或可见,则无关紧要。
以下是相关代码:
MapAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "Photo.h"
@interface MapAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
Photo *photo;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D)passCoordinate title:(NSString *)passTitle photo:(Photo *)passPhoto;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, retain) Photo *photo;
@end
MapAnnotation.m
#import "MapAnnotation.h"
@implementation MapAnnotation
@synthesize title;
@synthesize subtitle;
@synthesize photo;
@synthesize coordinate;
-(id)initWithCoordinate:(CLLocationCoordinate2D)passCoordinate title:(NSString *)passTitle photo:(Photo *)passPhoto
{
self = [super init];
if (self) {
// Custom initialization.
coordinate = passCoordinate;
title = passTitle;
photo = passPhoto;
}
return self;
}
- (void)dealloc {
[title release];
[photo release];
[super dealloc];
}
@end
MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "FlickrFetcher.h"
#import "Photo.h"
#import "Person.h"
#import "MapAnnotation.h"
#import "PhotoDetailViewController.h"
@interface MapViewController : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView *mapView;
FlickrFetcher *fetcher;
NSArray *fetchedObjects;
PhotoDetailViewController *photoDetail;
}
@property (retain, nonatomic) NSArray *fetchedObjects;
-(void)createAnnotations;
@end
和MapViewController.m
#import "MapViewController.h"
@implementation MapViewController
@synthesize fetchedObjects;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[mapView setDelegate:self];
fetcher = [FlickrFetcher sharedInstance];
// Setup a predicate that looks up all photos with non-zero latitide and longitude
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"longitude != 0 && latitude != 0"];
fetchedObjects = [fetcher fetchManagedObjectsForEntity:@"Photo" withPredicate:predicate];
[self createAnnotations];
}
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)createAnnotations
{
// iterate thorugh the array of photo objects, and setup annotation items, and put them on the map view
for ( Photo *currentPhoto in fetchedObjects )
{
MapAnnotation *annotation;
CLLocationCoordinate2D coordinate = {[[currentPhoto latitude] floatValue],[[currentPhoto longitude]floatValue]};
annotation = [[MapAnnotation alloc] initWithCoordinate:coordinate title:[currentPhoto name] photo:currentPhoto];
[mapView addAnnotation:annotation];
[annotation release];
}
}
#pragma mark -
#pragma mark Annotation View & Delegate Methods
- (MKAnnotationView *)mapView:(MKMapView *)thisMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MapAnnotation *myAnnotation = annotation;
MKPinAnnotationView *annotationView;
NSString* identifier = @"Pin";
MKPinAnnotationView* pin = (MKPinAnnotationView*)[thisMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(nil == pin) {
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease];
}
pin.animatesDrop = YES;
annotationView = pin;
UIButton *annotationButton=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = annotationButton;
[annotationView setEnabled:YES];
[annotationView setCanShowCallout:YES];
return annotationView;
}
- (void)mapView:(MKMapView *)thisMapView annotationView:(MKAnnotationView *)annotationView calloutAccessoryControlTapped:(UIControl *)control
{
// Get the annoation from the annotation view that was tapped and make it into our custom protocol
MapAnnotation *annotation = annotationView.annotation;
Photo *photo = [annotation photo];
photoDetail = [[PhotoDetailViewController alloc] initWithNibName:@"PhotoDetailViewController" bundle:[NSBundle mainBundle]];
[photoDetail setPassedPhoto:photo];
photoDetail.title = [NSString stringWithFormat:@"%@", [photo name]];
[self.navigationController pushViewController:photoDetail animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[photoDetail release];
}
#pragma mark -
#pragma mark Memory Cleanup
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[mapView release];
[super dealloc];
}
@end
这是回溯:
#0 0x01087057 in ___forwarding___ ()
#1 0x01086f22 in __forwarding_prep_0___ ()
#2 0x00eb9f81 in -[MKAnnotationContainerView _annotationViewForSelectionAtPoint:avoidCurrent:] ()
#3 0x00e89c99 in -[MKMapView handleTap:] ()
#4 0x005509c7 in -[UIGestureRecognizer _updateGestureWithEvent:] ()
#5 0x0054c9d6 in -[UIGestureRecognizer _delayedUpdateGesture] ()
#6 0x00552fa5 in _UIGestureRecognizerUpdateObserver ()
#7 0x010f6fbb in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#8 0x0108c0e7 in __CFRunLoopDoObservers ()
#9 0x01054bd7 in __CFRunLoopRun ()
#10 0x01054240 in CFRunLoopRunSpecific ()
#11 0x01054161 in CFRunLoopRunInMode ()
#12 0x01a4a268 in GSEventRunModal ()
#13 0x01a4a32d in GSEventRun ()
#14 0x002d642e in UIApplicationMain ()
#15 0x000023bc in main (argc=1, argv=0xbfffefd0) at /Users/chuck/Desktop/learning/flickr/main.m:14
答案 0 :(得分:2)
发布。
您的程序将在调试器中断,并且调用堆栈将在调试器UI中(或者您可以键入'bt
有了这个,崩溃的原因通常很明显。如果没有这个,我们就会批评代码。
所以,这就是......
Zombie调试给出:“* - [CFString length]:发送给deallocated的消息 实例0x5b7b250“。我的地图中没有任何地方 我使用的是视图或注释类 字符串长度。
是的,但您可能拥有代码创建的字符串,这些字符串会传递到系统框架以用于呈现目的或被复制或其他任何内容。而且,这些任务中的任何一个都需要字符串的长度。
调试僵尸时,查看特定对象的保留和释放历史记录会很有帮助,这可以在Allocations工具中找到。虽然在一个稍微不同的主题上,post I wrote中关于查找内存增加的一些屏幕截图和说明可能会有所帮助。
一个明显的错误是您没有保留title
传递给init
方法,但是您在dealloc
中发布了它。它应该保留; self.title = passTitle;
应该做到这一点。请注意,构建和分析应该捕获此类错误。 Photo
参数也是如此。
另请注意,NSString
属性通常应为copy
而非retain
。复制不可变字符串是免费的,复制可变字符串可以大大降低潜在的脆弱性。