[PATCH] D58514: Avoid needlessly copying blocks that initialize or are assigned to local auto variables to the heap

Duncan P. N. Exon Smith via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 13 16:21:33 PDT 2019


dexonsmith added a comment.

In D58514#1428434 <https://reviews.llvm.org/D58514#1428434>, @ahatanak wrote:

> Seems like the chromium code is valid and shouldn't crash. John/Erik what do you think? The following code also crashes with this patch applied.
>
>   typedef void (^BlockTy)();
>  
>   BlockTy sb;
>   __weak BlockTy wb;
>  
>   void foo(id a) {
>     auto b = ^{ NSLog(@"foo %@", a); };
>     wb = b; // block isn't copied to the heap.
>     sb = b; // block is copied to the heap.
>   }
>  
>   int main() {
>     auto x = [NSObject new];
>     foo(x);
>     sb();
>     wb();
>     return 0;
>   }
>


The assignment to `wb` seems like an escape of some sort.  What happens for this similar code?

  typedef void (^BlockTy)();
  
  BlockTy sb;
  __weak BlockTy wb;
  
  void bar(id b) {
    wb = b;
    sb = b;
  }
  
  void foo(id a) {
    bar(^{ NSLog(@"foo %@", a); });
  }
  
  int main() {
    auto x = [NSObject new];
    foo(x);
    sb();
    wb();
    return 0;
  }


Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D58514/new/

https://reviews.llvm.org/D58514





More information about the cfe-commits mailing list