When to call release on NSURLConnection delegate?

前端 未结 5 1987
挽巷
挽巷 2021-01-05 22:44

When passing a delegate to the a NSUrlConnection object like so:

[[NSURLConnection alloc] initWithRequest:request delegate:handler];
         


        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 23:28

    the object handler is used to implement connectionDidFinishLoading didReceiveData etc. I make a lot of calls to a number of web services and instead of creating one object for each, I have a central class for all that stuff:

    @interface DataService : NSObject {} 
    
    - (void) search:(NSString *) name byAddress:(NSString *)address; 
    
    @end 
    

    so the implementation of that method creates the delegate to pass:

    SearchDelegate *delegate = [[SearchDelegate alloc] init]; 
    [self sendRequestToUrl:urlString withJson:jsonString andHandler:delegate];
    

    what I am seeing in Instruments is that there is a memory leak on the SearchDelegate... so I think it is actually being retain'ed.

    Tinkering a bit I changed my sendRequestToUrlMethod to have this:

    // http code setup blah...
    
    [[NSURLConnection alloc] initWithRequest:request delegate:handler];
    [handler release];
    

    and this seems to have gotten ridden of memory leak being reported in Instruments.

提交回复
热议问题