gpt4 book ai didi

iphone - MKAnnotationView 因 EXC_BAD_ACCESS 崩溃

转载 作者:行者123 更新时间:2023-12-03 17:44:11 26 4
gpt4 key购买 nike

我知道 EXC_BAD_ACCESS 是难以确定的事情之一,并且我知道僵尸模式可以跟踪已释放的对象,但我仍然遇到了这个问题。

我有一个自定义 MKAnnotation,并且我在 map 上使用标准 AnnotationView。当我点击 map 上的图钉时,我崩溃了。如果我放大,只显示 1 个引脚,它就可以工作。如果我缩小并点击新的图钉,它就会崩溃。让我发疯的是,昨天这一切都100%有效(据我所知),而且我不记得更改过任何代码(诚然,我已经熬夜了 24 小时,所以……有可能我只是不记得做了它。)有一次,我看到一个图钉在崩溃之前给出了标题“com.apple.SOMETHING”。对我来说听起来像是过度发布,但我认为标题存储在注释中。

僵尸调试给出:

*** -[CFString length]: message sent to deallocated instance 0x5b7b250.

在我的 map View 或注释类中,我没有使用字符串长度。我确实有一张没有指定标题的照片,但当崩溃发生时,这张照片的图钉是否被点击或在屏幕上可见并不重要。

相关代码如下:

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

map 标注.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

最佳答案

当崩溃发生时,会有回溯。

发布。

您的程序将在调试器中中断,并且调用堆栈将位于调试器 UI 中(或者您可以键入“bt

这样一来,崩溃的原因通常就很明显了。如果没有这一点,我们就只能批评代码。

所以,这里......

<小时/>

Zombie debugging gives: "* -[CFString length]: message sent to deallocated instance 0x5b7b250". Nowhere in my map view or annotation class do I use string length.

是的,但是您的代码可能会创建一些字符串,这些字符串会传递到系统框架以用于渲染目的或进行复制或其他任何操作。而且,这些任务中的任何一项都可能需要字符串的长度。

调试僵尸时,查看特定对象的保留和释放历史记录会很有帮助,这可以在分配工具中找到。尽管主题略有不同,但 post I wrote 中的一些屏幕截图和说明关于查找内存增长可能会有所帮助。

一个明显的错误是,您没有保留传递给 init 方法的 title,而是在 dealloc 中释放它。应当保留; self.title = passTitle; 应该可以解决问题。请注意,构建和分析应该捕获此类错误。 Photo 参数也是如此。

另请注意,NSString 属性通常应为copy,而不是retain。复制不可变字符串是免费的,复制可变字符串大大降低了潜在的脆弱性。

关于iphone - MKAnnotationView 因 EXC_BAD_ACCESS 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4351656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com