释放对象

时间:2013-10-08 19:13:42

标签: ios objective-c memory-management

我有一个继承自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

1 个答案:

答案 0 :(得分:1)

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

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

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