Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TWRequest seems to leak when used in non-ARC projects

Instruments (Leaks) reports a memory leak when using TWRequest and I can't really see what I'm doing wrong.

Here are the steps to reproduce the issue:

Create a new Xcode project (ARC disabled), add the Twitter Framework and then just added the following lines to the code (e.g. in viewDidLoad):

TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"] parameters:nil requestMethod:TWRequestMethodGET];

[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    NSLog(@"in performrequest");

    [postRequest release];
}]; 

After profiling this code with Instruments (Leaks), it's telling me that the line with "performRequestWithHandler" is leaking:

Instruments screenshot

Marked line

Any ideas what to do to prevent this leak?

I found a similar question here but it seems to be unrelated to the problem I'm describing.

like image 359
kiteloop Avatar asked Nov 04 '22 10:11

kiteloop


1 Answers

I'm not sure why instruments picks this up as a leak but you can release the request outside of the completion block. Once the request is initiated, it is retained by the connection until the completion block is executed. Change your code to:

TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"] parameters:nil requestMethod:TWRequestMethodGET];

[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    NSLog(@"in performrequest");
}];

[postRequest release];

I wouldn't be surprised if this eliminates the leak as well.

like image 141
XJones Avatar answered Nov 15 '22 06:11

XJones