不确定在对象上调用释放的位置。救命?

时间:2011-03-25 21:52:42

标签: iphone objective-c memory-management

我有一个类可以处理iPhone上的所有网络访问。我用以下方式使用它:

-(void) randomFunction:
{
    // Assign params etc. <br />
    TTNetworkAccessLib *nLib = [[TTNetworkAccessLib alloc] initWithParams:..];
    [nLib createAndStartRequest];
    // Can't release here since the request is async and is still running.
}

-(void) requestSucceded:(NSData *) data
{
    // Can pass a reference to the request object and release here, but doesn't seem right?
}

-(void) requestFailed:(int) responseCode
{
    // Can pass a reference to the request object and release here, but doesn't seem right?
}

2 个答案:

答案 0 :(得分:2)

当我创建一个对象以异步方式执行某些操作时,我通常会在启动过程时保留它自己并在完成和完成任何回调后释放自己。这样,创建它的代码可以在以后不需要的时候释放它,并且我知道在该过程完成之前它不会被释放。

答案 1 :(得分:0)

有了这样的代表,我建议使用autorelease TNetworkAccessLib * nLib = [[[[TTNetworkAccessLib alloc] initWithParams:..] autorelease];