-1

我有一个继承自 MapKit 的 MKPlacemark 类的对象。我在 ViewController 的 viewDidLoad 期间有一个方法启动,它创建这样的对象(alloc + init)并将其传递给 MapView,如下所示

[self.mapView addAnnotation:<my instance of my class inheriting MKPlacemark>]

但是,当我启动我的程序时,我收到以下错误消息:

An instance 0x9a5d650 of class <name of my class> was deallocated while key value   
observers were still registered with it. Observation info was leaked, and may even 
become mistakenly attached to some other object. Set a breakpoint on 
NSKVODeallocateBreak to stop here in the debugger.

请注意,我使用 ARC。谁能告诉我如何避免这种重新分配?

谢谢 !

编辑:我的问题不是警告本身,而是我不希望那个对象在那一刻被释放......

EDIT2:类的代码如下

.h 文件看起来像这样

@interface OPTCreatureMark : MyMark

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

-(id)initWithCoordinate:(CLLocationCoordinate2D)coordinate; 
@end

像这样的.m

@implementation MyMark

@synthesize coordinate;

-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate_ {
    if (self = [super initWithCoordinate:coordinate_ addressDictionary:nil]) {

        self.coordinate=coordinate_;

        return self;
    } else {
        return nil;
    }
}
@end

4

1 回答 1

1

如果您确实在使用 KVO,听起来您需要在对象的dealloc方法中删除观察者,如下所示:

[self removeObserver:self.myDelegate forKeyPath:@"zoom"];

否则,消息可能会发送到您的类的已解除分配实例(由于已被解除分配而无法再响应),从而导致异常。

于 2013-10-08T19:32:35.067 回答