| 1 |
.. title:: clang-tidy - objc-nsinvocation-argument-lifetime |
| 2 |
|
| 3 |
objc-nsinvocation-argument-lifetime |
| 4 |
=================================== |
| 5 |
|
| 6 |
Finds calls to ``NSInvocation`` methods under ARC that don't have proper |
| 7 |
argument object lifetimes. When passing Objective-C objects as parameters |
| 8 |
to the ``NSInvocation`` methods ``getArgument:atIndex:`` and |
| 9 |
``getReturnValue:``, the values are copied by value into the argument pointer, |
| 10 |
which leads to incorrect releasing behavior if the object pointers are |
| 11 |
not declared ``__unsafe_unretained``. |
| 12 |
|
| 13 |
For code: |
| 14 |
|
| 15 |
.. code-block:: objc |
| 16 |
|
| 17 |
id arg; |
| 18 |
[invocation getArgument:&arg atIndex:2]; |
| 19 |
|
| 20 |
__strong id returnValue; |
| 21 |
[invocation getReturnValue:&returnValue]; |
| 22 |
|
| 23 |
The fix will be: |
| 24 |
|
| 25 |
.. code-block:: objc |
| 26 |
|
| 27 |
__unsafe_unretained id arg; |
| 28 |
[invocation getArgument:&arg atIndex:2]; |
| 29 |
|
| 30 |
__unsafe_unretained id returnValue; |
| 31 |
[invocation getReturnValue:&returnValue]; |
| 32 |
|
| 33 |
The check will warn on being passed instance variable references that have |
| 34 |
lifetimes other than ``__unsafe_unretained``, but does not propose a fix: |
| 35 |
|
| 36 |
.. code-block:: objc |
| 37 |
|
| 38 |
// "id _returnValue" is declaration of instance variable of class. |
| 39 |
[invocation getReturnValue:&self->_returnValue]; |