Objective-C out of scope问题
Objective-C有个很脑残的问题
描述如下:
ASIFormRequest类是ASIHTTPRequest类的子类。在函数中,对另一地方返回的ASIFormRequest指针进行retain,出了这个函数,retain的指针可以访问,但是所有从父类继承的属性、方法,都out of scope,一使用就程序崩溃。
解决办法很简单,retain时不要retain子类指针,retain父类的指针既可。
-(ASIHTTPRequest *) _getSinaRequest{
ASIHTTPRequest *request = [sinaApi uploadWithImage:selectedPhoto status:text.text];
request.delegate = self;
request.showAccurateProgress = YES;
request.uploadProgressDelegate = sinaPostProgress;
[request retain];
return request;
ASIHTTPRequest *request = [sinaApi uploadWithImage:selectedPhoto status:text.text];
request.delegate = self;
request.showAccurateProgress = YES;
request.uploadProgressDelegate = sinaPostProgress;
[request retain];
return request;
}
- (void)uploadPhoto
{
if ([sinaApi isAuthorized]) {
sinaPosted = NO;
sinaPostRequest = [self _getSinaRequest];
[sinaPostRequest startAsynchronous];
}
else {
sinaPosted = YES;
}
//... ...
}
- (void)cancel: (id)sender{
if(sinaPostRequest){
if(![sinaPostRequest isFinished]){ // 如果retain的是ASIFormRequest指针,这里马上死掉
[sinaPostRequest clearDelegatesAndCancel];
}
[sinaPostRequest release];
}
sinaPostRequest = nil;
[progressWindow hideImmediately];
{
if ([sinaApi isAuthorized]) {
sinaPosted = NO;
sinaPostRequest = [self _getSinaRequest];
[sinaPostRequest startAsynchronous];
}
else {
sinaPosted = YES;
}
//... ...
}
- (void)cancel: (id)sender{
if(sinaPostRequest){
if(![sinaPostRequest isFinished]){ // 如果retain的是ASIFormRequest指针,这里马上死掉
[sinaPostRequest clearDelegatesAndCancel];
}
[sinaPostRequest release];
}
sinaPostRequest = nil;
[progressWindow hideImmediately];
}