0
__weak NSBlockOperation *secondBlockOperation  = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"%@",secondBlockOperation);
    NSLog(@"this is the second block");
}];
__block __weak NSBlockOperation *secondBlockOperation  = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"%@",secondBlockOperation);
    NSLog(@"this is the second block");
}];
__block  NSBlockOperation *secondBlockOperation  = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"%@",secondBlockOperation);
    NSLog(@"this is the second block");
}];

This is the code I cannot understand.I have found that the __block one will cause the memory leak.So the __block __weak one come into my mind.OK,there is no more leak at all.But when I custom a class with a block as a strong property,like this __weak Person *one = nil; one = [[Person alloc] initWithBlock:^{ NSLog(@"%@",one); }]; the weak assign warning showed.

To be honest,I don't understand the result when __block and __weak used together.__block value will be a pointer to a struct with forwarding pointer and the value pointer in it .So the weak is worked for the pointer to the __block struct or the pointer in the __block struct.and why __weak NSBlockOperation *secondBlockOperation have no warning??!

1 Answers1

-1

All block operations should generally be weak. Because, if the block operation is done, there's no need to keep this block object in memory. As written in apple docs (https://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSOperation_class/index.html#//apple_ref/occ/cl/NSOperation):

An operation object is a single-shot object—that is, it executes its task once and cannot be used to execute it again

Karaban
  • 151
  • 8