According to Clang source code for generating code of Objective-C blocks.
Objective-C blocks literal is generated by EmitBlockLiteral
function.
llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
LLVM document explains deeply what is Block literal. Anyway this function generates a block descriptor and a copy helper function of the specified block. The copy helper function is for capturing auto variables and self
.
buildBlockDescriptor -> buildCopyHelper -> GenerateCopyHelperFunction
In GenerateCopyHelperFunction
function, Clang emits objc_storeStrong
for each Objective-C object auto variable that will be captured by the block.
for (const auto &CI : blockDecl->captures()) {
...
EmitARCStoreStrongCall(...
So, this line would count up the retain count of self
(1 -> 2).
After that, EmitBlockLiteral
function emits objc_retain
for each Objective-C object auto variable that will be captured by the block, as well.
// Next, captured variables.
for (const auto &CI : blockDecl->captures()) {
...
EmitExprAsInit -> EmitScalarInit -> EmitARCRetain
Therefore this line would count up the retain count of self
too (2 -> 3).
I don't know the exact reason. But apparently, there is some reason to retain Objective-C object before capturing object by the block copy helper function.