3
votes

I am using ARC and in one method I alloc an operation object. Will that cause a memory leak? If yes where should I set it to nil? Inside the completion blocks or after the [operation start]; ?

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    // success downloading file
    // Do something
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // error downloading file
    // Do something else
}];

[operation start];
2

2 Answers

1
votes

No leak. It's just fine. However, typically, referring to operation within the block will cause a retain cycle. But it looks like that API anticipated this for you and passes a usable copy. I guess you can assume that the operation block parameter is safe to refer to.

Typically if you need to refer to the block owner within the block, you would declare an unretained copy and refer to that instead...

__unsafe_unretained BlockOwningClass *blockOwnerCopy = blockOwner;
[blockOwner doBlock:^{  NSLog(@"%@", blockOwnerCopy); }];
1
votes

No, in this case the system will take care of the memory management for you without any risk of leaking.