TWRequest seems to leak when used in non-ARC projects

574 views Asked by At

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.

1

There are 1 answers

0
XJones On

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.