我有下一段代码,一个具有此属性的 iVar 保留并在其类dealloc
方法中释放。iVar 用于 2 种方法并不断更改值,但有时当我使用时值已损坏。这是为什么?
。H
@interface ChatController : NSObject <ASIHTTPRequestDelegate>{
NSTimer *timer;
NSString *_idLastMessageFromServer;
}
@property(nonatomic, retain)NSString *idLastMessageFromServer;
@end
.m
@implementation ChatController
@synthesize idLastMessageFromServer = _idLastMessageFromServer;
- (void)initLoopTimer{
timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(update:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)update:(id)sender{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:CONSTANT_YYYY];
[request setDelegate:self];
[request addPostValue:_idLastMessageFromServer forKey:CONSTANT_XXX];
[request setDidFinishSelector:@selector(requestUpdateFinish:)];
[request startAsynchronous];
}
- (void)requestUpdateFinish:(ASIHTTPRequest *)request{
NSString *response = [request responseString];
if(response && response.length){
if(![response isEqualToString:CHAT_RESPONSE_NO_MESSAGES]){
NSArray *array = [response componentsSeparatedByString:CHAT_PARSE_RESPONSE];
if(array && [array count] == 2){
**_idLastMessageFromServer = [array objectAtIndex:0];**
}
}
}
}
但是当循环调用方法update:时,它在这行代码中崩溃了
[request addPostValue:_idLastMessageFromServer forKey:CONSTANT_XXX];
带有 EXC_BAD_ACCESS 消息,但为什么呢?