beng

ARC bug with objc_precise_lifetime and ternary operator

Nov 1st, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. % clang -v
  2. Apple LLVM version 5.0 (clang-500.2.75) (based on LLVM 3.3svn)
  3. Target: x86_64-apple-darwin12.4.0
  4. Thread model: posix
  5.  
  6. % cat testARC.m
  7. #import <Foundation/Foundation.h>
  8.  
  9. static BOOL gSomeValue;
  10.  
  11. static BOOL DoSomeCheck(void)
  12. {
  13.   return gSomeValue;
  14. }
  15.  
  16. @interface Scoped : NSObject
  17. @end
  18.  
  19. @implementation Scoped
  20.  
  21. - (instancetype)init
  22. {
  23.   self = [super init];
  24.   NSLog(@"%s %@", __func__, self);
  25.   return self;
  26. }
  27.  
  28. - (void)dealloc
  29. {
  30.   NSLog(@"%s %@", __func__, self);
  31. }
  32.  
  33. @end
  34.  
  35. int main(int argc, char **argv)
  36. {
  37.   gSomeValue = YES;
  38.  
  39.   {
  40.     __attribute__((objc_precise_lifetime)) Scoped *scoped = [[Scoped alloc] init];
  41.     NSLog(@"At the end of first block");
  42.   }
  43.  
  44.   {
  45.     __attribute__((objc_precise_lifetime)) Scoped *scoped = DoSomeCheck() ? [[Scoped alloc] init] : nil;
  46.     NSLog(@"At the end of second block");
  47.   }
  48.  
  49.   {
  50.     Scoped *scopeMe = DoSomeCheck() ? [[Scoped alloc] init] : nil;
  51.     __attribute__((objc_precise_lifetime)) Scoped *scoped = scopeMe;
  52.     NSLog(@"At the end of third block");
  53.   }
  54. }
  55.  
  56. % clang -fobjc-arc -O0 -o testARC-dbg ./testARC.m
  57. % clang -fobjc-arc -Os -o testARC-opt ./testARC.m
  58.  
  59. % ./testARC-dbg                                  
  60. 2013-11-01 16:53:27.387 testARC-dbg[85843:707] -[Scoped init] <Scoped: 0x7f9102c081c0>
  61. 2013-11-01 16:53:27.389 testARC-dbg[85843:707] At the end of first block
  62. 2013-11-01 16:53:27.389 testARC-dbg[85843:707] -[Scoped dealloc] <Scoped: 0x7f9102c081c0>
  63. 2013-11-01 16:53:27.390 testARC-dbg[85843:707] -[Scoped init] <Scoped: 0x7f9103b009d0>
  64. 2013-11-01 16:53:27.390 testARC-dbg[85843:707] At the end of second block
  65. 2013-11-01 16:53:27.390 testARC-dbg[85843:707] -[Scoped dealloc] <Scoped: 0x7f9103b009d0>
  66. 2013-11-01 16:53:27.391 testARC-dbg[85843:707] -[Scoped init] <Scoped: 0x7f9102c02b40>
  67. 2013-11-01 16:53:27.408 testARC-dbg[85843:707] At the end of third block
  68. 2013-11-01 16:53:27.412 testARC-dbg[85843:707] -[Scoped dealloc] <Scoped: 0x7f9102c02b40>
  69.  
  70. % ./testARC-opt
  71. 2013-11-01 16:53:13.479 testARC-opt[85825:707] -[Scoped init] <Scoped: 0x7ff758c081a0>
  72. 2013-11-01 16:53:13.484 testARC-opt[85825:707] At the end of first block
  73. 2013-11-01 16:53:13.485 testARC-opt[85825:707] -[Scoped dealloc] <Scoped: 0x7ff758c081a0>
  74. 2013-11-01 16:53:13.486 testARC-opt[85825:707] -[Scoped init] <Scoped: 0x7ff759b00470>
  75. 2013-11-01 16:53:13.486 testARC-opt[85825:707] -[Scoped dealloc] <Scoped: 0x7ff759b00470>
  76. 2013-11-01 16:53:13.487 testARC-opt[85825:707] At the end of second block
  77. 2013-11-01 16:53:13.489 testARC-opt[85825:707] -[Scoped init] <Scoped: 0x7ff759900140>
  78. 2013-11-01 16:53:13.489 testARC-opt[85825:707] At the end of third block
  79. 2013-11-01 16:53:13.490 testARC-opt[85825:707] -[Scoped dealloc] <Scoped: 0x7ff759900140>
Advertisement
Add Comment
Please, Sign In to add comment