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

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 13 16:31:42 PDT 2019


ahatanak added a comment.

In D58514#1428495 <https://reviews.llvm.org/D58514#1428495>, @dexonsmith wrote:

> 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;
>   }
>


That code doesn't crash because the block is retained at the entry of `bar` and ARC optimizer doesn't remove the retain/release pairs in `bar`.


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