I am wondering if I am using blocks as shown in the code below
__block Loader *loader = [[Loader alloc]initWithResourcePath:self.resourcePath];
[loader setCompletionHandler:^(NSArray *anArray){
self.answerArray=anArray;
[self reloadData];
}];
[loader getObjects];
My question is with regards to memory management. The analyzer tells me that there is a potential leak (since I did an alloc/init for my loader). How can I stop the leak here? I tried to release the loader at the end but that causes my app to stop functioning. Any advise is appreciated here
Several issues:
there is no reason for
loaderto be declared__block; you aren’t re-assigning in the block and, thus, the__blockis pointless.the leak is because you
alloc/initthe loader, but never release itdo not name methods
getSomething; thegetprefix is reserved for methods that return stuff by reference. Just call itobjects. If it is supposed to trigger the load, then call itloadorperformLoad.If it is asynchronous, then
getObjectsis pointless. If synchronous, then the completion block is pointless.If
loaderis to be used synchronously,releaseit at the end of the method. If it isasynchronous, then the completion block could release it. Note that the use of__blockin this case is still pointless; while referring toloaderin the completion block will create a retain cycle, it will be broken when you explicitly Block_release() the block in yourloader(because you must have done aBlock_copy()when setting the completion handler if it is to be used asynchronously in the first place).