Menu

#631 Enable and resolve warning "Implicit retain of 'self' within blocks"

iphone_1.10
open
nobody
None
6
2020-06-13
2020-06-13
No

Project / Build settings / Basic, near the bottom of the page.
The warnings should be analysed carefully if they can introduce race conditions.

Discussion

  • Christa Runge

    Christa Runge - 2020-06-13

    See also svn r3929 and r3930 where more such warnings were intentionally introduced, but then the compiler warning was (temp) deactivated.

     
  • Christa Runge

    Christa Runge - 2020-06-13

    Apple docu - Working with blocks:
    https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html#//apple_ref/doc/uid/TP40011210-CH8-SW16

    • Blocks can also take arguments and return values just like methods and functions.
    • Blocks Can Capture Values from the Enclosing Scope
    • the value is captured when the block is defined.
    • Only the value is captured, unless you specify otherwise.
    • the block cannot change the value of the original variable, or even the captured value (it’s captured as a const variable).
    • If you need to be able to change the value of a captured variable from within a block, you can use the 'block' storage type modifier on the original variable declaration. This means that the variable lives in storage that is shared between the lexical scope of the original variable and any blocks declared within that scope. It also means that the block can modify the original value.
    • You Can Pass Blocks as Arguments to Methods or Functions
    • Blocks are also used for callbacks, defining the code to be executed when a task completes.
    • A Block Should Always Be the Last Argument to a Method (best practice); because it makes the method call easier to read if it uses a literal as argument for the block.
    • Avoid Strong Reference Cycles when Capturing self. If you need to capture self in a block, such as when defining a callback block, it’s important to consider the memory management implications.
    • Blocks Can Simplify Concurrent Tasks
     

    Last edit: Christa Runge 2020-06-13

Log in to post a comment.