Strange memory leak using blocks and copying in a certain way to ARC

The following simple ARC code in the latest Xcode (4.6.2) shows leaks when profiling with the Leaks tool in the iOS simulator in the Release build configuration with -Os optimization:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { for (int i = 0; i < 10; i++) { void (^block)() = ^ { NSLog(@"%d", i); }; id x = block; [x copy]; } return YES; } 

It shows 10 leaked blocks. (It does not show a leak with optimization disabled, but the optimization for our applications is not realistic.) Strange, looking at the memory management history for each of the leaked blocks, they look fine - each one has a malloc from the copy and release; but does the release somehow not release it?

Is this a compiler error (using the default Apple LLVM compiler)?

+4
source share
1 answer

It seems to be either a bug in the compiler or a leak tool, I'm not sure what. You must indicate an error with Apple.

The same thing happens with code reduction:

 int i = 0; void (^block)() = ^{ int y = i; }; id x = block; 

A leak appears only if the block refers to a local variable from the parent scope.

Do you see this happening in real production code, or just in this example?

0
source

Source: https://habr.com/ru/post/1479100/


All Articles