Tejaswi Yerukalapudi
Tejaswi Yerukalapudi

Reputation: 9157

Unsure where to call release on an Object. Help?

I have a class that handles all the Network Access on the iPhone. And I use it in the following way:

-(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?
}

Upvotes: 0

Views: 51

Answers (2)

ughoavgfhw
ughoavgfhw

Reputation: 39905

When I create an object to do something asynchronously, I usually have it retain itself when starting the procedure and release itself after finishing and completing any callbacks. This way, the code creating it can release it if it doesn't need it later, and I know it will not be deallocated until the process has completed.

Upvotes: 2

Al Pascual
Al Pascual

Reputation: 1241

With a delegate like that I would recommend to use the autorelease TNetworkAccessLib *nLib = [[[TTNetworkAccessLib alloc] initWithParams:..] autorelease];

Upvotes: 0

Related Questions