簡體   English   中英

MKAnnotationView因EXEC_BAD_ACCESS而崩潰

[英]MKAnnotationView crashing with EXEC_BAD_ACCESS

我知道EXC_BAD_ACCESS是難以確定的事情之一,我知道僵屍模式可以追蹤解除分配的對象,但我仍然遇到這個問題。

我有一個自定義的MKAnnotation ,我在我的地圖上使用標准的AnnotationView。 當我點擊地圖上的大頭針時,我崩潰了。 如果我放大所以只顯示1個引腳,它就可以工作。 如果我縮小並點擊新的圖釘,它會崩潰。 讓我抓狂的是,昨天(據我所知)這是100%有效的,而且我不記得更改任何代碼(坦白地說,我已經工作了24小時,所以...我可能不記得這樣做了)它。)有一次,我看到一個引腳在崩潰之前給出了標題“com.apple.SOMETHING”。 聽起來像對我的過度釋放,但標題存儲在注釋中,我想。

僵屍調試提供:

*** - [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

發生崩潰時,會有回溯。

發表它。

您的程序將在調試器中中斷,並且調用堆棧將位於調試器UI中(或者您可以鍵入'bt

因此,崩潰的原因通常非常明顯。 如果沒有這個,我們就會批評代碼。

所以,這...


僵屍調試給出:“ *-[CFString length]:發送到已釋放實例0x5b7b250的消息”。 我的地圖視圖或注釋類中的任何地方都不會使用字符串長度。

是的,但您可能擁有代碼創建的字符串,這些字符串會傳遞給系統框架以用於呈現目的或被復制或其他任何內容。 而且,可能其中任何一項任務都需要字符串的長度。

在調試僵屍時,查看特定對象上的保留和釋放的歷史記錄很有用,這在分配工具中可用。 雖然在一個稍微不同的主題上, 我寫的關於查找內存增加的帖子中的一些屏幕截圖和說明可能會有所幫助。

一個明顯的錯誤是你沒有保留傳入init方法的title ,但你在dealloc釋放它。 應該保留它; self.title = passTitle; 應該做的伎倆。 請注意, 構建和分析應捕獲此類錯誤。 Photo參數也是如此。

另請注意, NSString屬性通常應該是copy而不是retain 復制不可變字符串是免費的,復制可變字符串可以大大降低潛在的脆弱性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM