开发者

dealloc problem with instance variables

A problem happens when I was trying to release one of my instance variables and reassign it a new value.

I would like to release the address that a instance variable points to, and re-assign a new value to it.

The code look like this: The .h

@interface MapPageController : UIViewController<MKMapViewDelegate> {
 AddressAnnotationManager *addAnnotation;
}
- (IBAction) showAddress;
@property (nonatomic, retain) AddressAnnotationManager *addAnnotation;

The .m

@synthesize addAnnotation;
- (IBAction) showAddress {
        if(addAnnotation != nil) {
  [mapView removeAnnotation:addAnnotation];
  [addAnnotation release]; // this generates the problem
  开发者_StackOverflowaddAnnotation = nil;
 }
 addAnnotation = [[AddressAnnotationManager alloc] initWithCoordinate:location];
 addAnnotation.pinType = userAddressInput;
 addAnnotation.mSubTitle = addressField.text;
}

However, with [addAnnotation release], a EXC_BAD_ACCESS always comes along if the process runs through it.

Thus I printed out the memory address in the dealloc of AddressAnnotationManager:

- (void)dealloc {
 NSLog(@"delloc Instance: %p", self);
 [super dealloc];
}

I turned on Zombie, the console gave me something like this:

2010-10-10 17:02:35.648 [1908:207] delloc Instance: 0x46c7360

2010-10-10 17:02:54.396 [1908:207] -[AddressAnnotationManager release]: message sent to deallocated instance 0x46c7360*

It means the code reaches dealloc before the problem occurs.

I have checked all the possible places where I could release addAnnotation. However, I could not find any.

Does anyone happen to find what the problem is?


I suspect that this is not the whole code involving the addAnnotation variable. Most likely [mapView removeAnnotation:addAnnotation];, which releases addAnnotation, already makes the reference count drop to zero. Do you have something like this in your code somewhere ?

 [mapView addAnnotation:addAnnotation];
 [addAnnotation release];

If so, then you have transfered the complete ownership of addAnnotation to the mapView and you don't need to release it in showAddress any more, which means that removeAnnotation: is enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜