Tom Redman
Tom Redman

Reputation: 5650

Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called

I have a separate class I want to handle the response from an ASIHTTPRequest, so I create an instance of the class, then pass it to my request. The problem is that my delegate class is being deallocated before the request finishes.

I'm using ARC. How can I ensure the delegate is retained for the didFinish/didFail selectors?

-(void)getStuff
{
    NSURL *url = [NSURL URLWithString:@"http://example.com/json"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

    UpdateFeedDelegate *updateFeedDelegate = [[UpdateFeedDelegate alloc] init];

    [request setDelegate:updateFeedDelegate];
    [request setDidFinishSelector:@selector(updateTestDone:)];
    [request setDidFailSelector:@selector(getSingleUpdateFeedBackgroundRequestFailed:)];

    [request startAsynchronous];
}

In the example, the selector classes exist as expected. I get the error:

*** -[UpdateFeedDelegate respondsToSelector:]: message sent to deallocated instance 0x56efb50

*** NSInvocation: warning: object 0x56efb50 of class '_NSZombie_UpdateFeedDelegate' does not implement methodSignatureForSelector: -- trouble ahead

*** NSInvocation: warning: object 0x56efb50 of class '_NSZombie_UpdateFeedDelegate' does not implement doesNotRecognizeSelector: -- abort

Upvotes: 1

Views: 507

Answers (1)

Dan F
Dan F

Reputation: 17732

Delegates are typically held by weak or assign properties. You need to hold onto a strong or retain reference to your UpdateFeedDelegate somewhere in order for it to not get autoreleased. Typically, I hold this in the class that is creating the request and delegate.

Upvotes: 2

Related Questions