<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Objective-C block retains and never releases captured parameter under ARC"
   href="https://bugs.llvm.org/show_bug.cgi?id=43735">43735</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Objective-C block retains and never releases captured parameter under ARC
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>9.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ronlittle2@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=22692" name="attach_22692" title="compile foo.m with -fobjc-arc and see foo.s">attachment 22692</a> <a href="attachment.cgi?id=22692&action=edit" title="compile foo.m with -fobjc-arc and see foo.s">[details]</a></span>
compile foo.m with -fobjc-arc and see foo.s

In this method, when compiling with -fobjc-arc, the block captures
'someParameter' and clang issues a retain call with no matching release, and
there are no warnings or errors. The compiled code will always cause
'someParameter' to leak, and this kind of memory leak is easy to introduce and
can be very difficult to track down in a large project.

- (void)foo:(Foo*)someParameter {
    [Bar bar:^{
        [someParameter foo2];
    }];
}

The behavior that we were expecting was that when the block is released, that
the block's deallocator would release all captured variables.

If the current behavior is by design or is too difficult to improve, then the
next most desirable result would be that a compile error would be issued,
saying that 'someParameter' can't be used in a block and that either a weak
referencing variable needs to be used instead or the code needs to be compiled
without -fobjc-arc.

clang.exe -dumpversion reports 4.2.1
I tried out Clang 9.0.0 and looked at the generated .s file (using -save-temps)
and it appears that it would have the same problem, as there is only one call
to 'retain' and no calls to 'release'. In production, where we found the
problem, we use Clang 4.2.1.

Here are all the compiler options we use. Please see the generated foo.s.

clang.exe -c --target=i686-pc-windows-msvc -fasm-blocks -fblocks
-fobjc-runtime=ios-6.0.0 -fdiagnostics-format=msvc -fms-extensions
-fms-compatibility -fno-delayed-template-parsing -fexceptions -frtti
-fno-strict-aliasing -fno-omit-frame-pointer -fstack-protector -fno-short-enums
-g -gcodeview  -O0 -fno-inline -D MSVC_LINK -D WIN32 -D _WIN32 -D _CONSOLE
-DMICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS=1
-DFIXME_ASSERTS=0 -DAPPORTABLE=1 -D__APPORTABLE__ -DNO_WRAPS=1
-D_ALLOW_KEYWORD_MACROS -DNOMINMAX -U_DLL -D_MT=1 -Wshorten-64-to-32
-Wno-deprecated-declarations -Wswitch -Wparentheses -Wformat -Wunused-value
-Wunused-variable -Wincompatible-pointer-types -Wno-unknown-pragmas
-Werror=implicit-function-declaration -Wno-unused-local-typedef      
-Wno-newline-eof -Werror-return-type -Wno-newline-eof -Wno-c++11-narrowing
-Wempty-body -Wno-ignored-pragma-intrinsic -Wno-ignored-attributes
-Wno-nonportable-include-path -Wno-pragma-pack -Wno-microsoft-anon-tag
-Wno-missing-prototype-for-cc -Wno-unused-value -g -fobjc-arc -save-temps -o
foo.m.obj -c foo.m</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>