Using blocks iPhone application in iPhone Simulator 4.3 / Xcode 4.2 and 4.0.2

Does anyone else have problems with 4.3 iPhone Simulator in Xcode 4.2 (lion) or 4.0.2?

I have code that has been working for a long time, is also being tested in production, which uses blocks to indicate completion actions. For example, I use UIView animation to gradually reduce text over the label as follows:

[UIView animateWithDuration: 0.0 delay: 0.0 options: (UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionNone) animations: ^{ videoTextLabel1.alpha = 0.0; videoTextLabel2.alpha = 0.0; videoTextLabel3.alpha = 0.0; } completion: ^(BOOL completed) { [self fadeInNextMeditationLine: 0]; }]; 

I confidently get EXEC_BAD_ACCESS in the simulator - never a problem on the device.

Elsewhere, I use my own implementation of the blocking block to take action after the user rejects the modal view.

  ValuePickerController *controller = [[ValuePickerController alloc] initWithNibName: kValuePickerXIBFileName bundle: nil labelText: @"prompt") value: alertSettings.frequency minimumValue: kMinimumFrequency maximumValue: kMaximumFrequency completionBlock: ^(NSInteger newValue) { [self updateFrequencyText: newValue]; [self changeFrequencySetting]; }]; 

There are no NSZombies, and the analyzer works clean. Plus, this code has been in production for 6 months without a glitch.

Does anyone else have such a problem? This has been happening since I updated Xcode.

+7
source share
1 answer

As far as I know, this is a known issue that only affects simulator 4.3. Version 4.2 and the preliminary version 5.0 do not seem to demonstrate this problem. However, now this is a big problem when Lion is missing, because the latest version of Xcode with the general release supports only the 4.3 simulator where this problem occurs.

The actual reason is the hooks between the Blocks and ObjC loops. The blocks themselves will work fine, but any attempt to call an Objective-C message on them will result in segfault. This is because the Blocks runtime contains a couple of uninitialized references to the corresponding ObjC classes, and on the iOS 4.3 simulator they are never initialized when the ObjC runtime is loaded (they are initialized only if ObjC is used at all) Blocks runtime does not depend from downloading Foundation). You can verify this at run time by looking at the values ​​for _NSConcreteStackBlock , _NSConcreteGlobalBlock and _NSConcreteMallocBlock in the debugger. In simulator 4.2 or on the device, these values ​​will not be zero, but on simulator 4.3 they will still be zero.

I have a potential solution that I will refer to here if necessary, but first I’m going to try to compress some information from Apple about whether they have a fix on the release release or if they need some more information, etc.

UPDATE: SOLVING THE PROBLEM

I have torn a lot, and this ultimately boils down to the following: don't use weak coupling with libSystem.dylib using -weak_library . Instead, you should either not use the weak libSystem link at all (I had to support iOS 3.1.x because the Blocks code code generated by the compiler in some conditional code dependent on iOS4 caused a link error during startup, i.e. a bad crash), or you should use -weak lSystem instead, which the Simulator understands better.

When you work in iOS Simulator, you can see the loaded libraries (in Xcode: "Product-> Debug-> Shared Libraries ..."), and if you look for "Blocks", you will see two elements: libsystem_blocks.dylib and libsystem_sim_blocks.dylib . The latter is the one associated with CoreFoundation, which initializes ObjC runtime gluing for the Blocks runtime. However, since you loosely libSystem library as a whole, the characters that are usually overridden by the Simulator version (since it is loaded later by libSystem) are actually overwritten at run time from the first library that implements them. This means that you will find system versions of _NSConcreteGlobalBlock and friends who have not been initialized with the custom version of ObjC for Simulator.

For (a lot!) More information about the problem, and to see how I track it, look at the thread I made on the Apple developer forums .

+22
source

All Articles