cocos2d-2.0-x-2.0.4 Crashing on App Store

Hi,

I am having a strange issue. I recently did some updates to my application and submitted to the appstore a new version of the app. The core stuff of my app was not changes I just added social stuff (facebook twitter) and a new screen that I build with cocosbuilder. Before submitting my app I did a full test on my test group of devices (iphone 3g, 4s, ipad) everything works. I submitted my app it got approved and the update rolled out. Now I updated the application and I find out that it crashes right on my splash screen which is a ccbi file.Here is the device crash log:

0   Color-Shift                     0x000dd71c cocos2d::extension::CCBReader::readFloat() (CCBReader.cpp:422)
1   Color-Shift                     0x000dde52 cocos2d::extension::CCBReader::readKeyframe(int) (CCBReader.cpp:648)
2   Color-Shift                     0x000dd9ae cocos2d::extension::CCBReader::readNodeGraph(cocos2d::CCNode*) (CCBReader.cpp:509)
3   Color-Shift                     0x000ddc20 cocos2d::extension::CCBReader::readNodeGraph(cocos2d::CCNode*) (CCBReader.cpp:586)
4   Color-Shift                     0x000dd342 cocos2d::extension::CCBReader::readFileWithCleanUp(bool) (CCBReader.cpp:689)
5   Color-Shift                     0x000dd2ec cocos2d::extension::CCBReader::readNodeGraphFromData(cocos2d::extension::CCData*, cocos2d::CCObject*, cocos2d::CCSize const&, cocos2d::extension::CCBAnimationManager**) (CCBReader.cpp:230)
6   Color-Shift                     0x000dd1aa cocos2d::extension::CCBReader::readNodeGraphFromFile(char const*, cocos2d::CCObject*, cocos2d::CCSize const&, cocos2d::extension::CCBAnimationManager**) (CCBReader.cpp:213)
7   Color-Shift                     0x000dd0a4 cocos2d::extension::CCBReader::readNodeGraphFromFile(char const*, cocos2d::CCObject*) (CCBReader.cpp:192)
8   Color-Shift                     0x000fdb50 IntroScene::init() (IntroScene.cpp:55)

Now googling I found that there was a memory alignment issue that was fixed in the branch on git. So I copy and pasted this:

 float * pF = (float*)(this->mBytes + this->mCurrentByte);
            float f = 0;

            // N.B - in order to avoid an unaligned memory access crash on 'memcpy()' the the (void*) casts of the source and
            // destination pointers are EXTREMELY important for the ARM compiler.
            //
            // Without a (void*) cast, the ARM compiler makes the assumption that the float* pointer is naturally aligned
            // according to it's type size (aligned along 4 byte boundaries) and thus tries to call a more optimized
            // version of memcpy() which makes this alignment assumption also. When reading back from a file of course our pointers
            // may not be aligned, hence we need to avoid the compiler making this assumption. The (void*) cast serves this purpose,
            // and causes the ARM compiler to choose the slower, more generalized (unaligned) version of memcpy()
            //
            // For more about this compiler behavior, see:
            // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3934.html
            memcpy((void*) &f, (const void*) pF, sizeof(float));

            this->mCurrentByte += sizeof(float);
            return f;

And compiled tested and resubmitted to apple with a request to expedite to fix the runtime crash. And guess what the app got approved processed and when I download the app still crashes:

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: EXC_ARM_DA_ALIGN at 0x1f0807c9
Crashed Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   Color-Shift                     0x000f971c cocos2d::extension::CCBReader::readFloat() (CCBReader.cpp:422)
1   Color-Shift                     0x000f9e52 cocos2d::extension::CCBReader::readKeyframe(int) (CCBReader.cpp:648)
2   Color-Shift                     0x000f99ae cocos2d::extension::CCBReader::readNodeGraph(cocos2d::CCNode*) (CCBReader.cpp:509)
3   Color-Shift                     0x000f9c20 cocos2d::extension::CCBReader::readNodeGraph(cocos2d::CCNode*) (CCBReader.cpp:586)
4   Color-Shift                     0x000f9342 cocos2d::extension::CCBReader::readFileWithCleanUp(bool) (CCBReader.cpp:689)
5   Color-Shift                     0x000f92ec cocos2d::extension::CCBReader::readNodeGraphFromData(cocos2d::extension::CCData*, cocos2d::CCObject*, cocos2d::CCSize const&, cocos2d::extension::CCBAnimationManager**) (CCBReader.cpp:230)
6   Color-Shift                     0x000f91aa cocos2d::extension::CCBReader::readNodeGraphFromFile(char const*, cocos2d::CCObject*, cocos2d::CCSize const&, cocos2d::extension::CCBAnimationManager**) (CCBReader.cpp:213)
7   Color-Shift                     0x000f90a4 cocos2d::extension::CCBReader::readNodeGraphFromFile(char const*, cocos2d::CCObject*) (CCBReader.cpp:192)
8   Color-Shift                     0x00119b50 IntroScene::init() (IntroScene.cpp:55)

Any ideas what I should do? Anyone else recently submitted an app using cocos2d-2.0-x-2.0.4?

Thanks

I forgot to also mention I did modifie Other linker Flags and added these 2:
~~lsqlite3.0
~~ObjC

Not sure exactly why this works when I compile a release version from xcode and deploy to a device no problems but when it goes threw apples certification its passing and once it gets downloaded it crashes. Wouldnt apple reject an app that crashes on run time?

I got the same problem when I published the app in ad-hoc build. Here is my post in stack overflow:

The app is no problem in device even I built it in release mode which is exactly the same as in ad-hoc mode. But It crashes in ad-hoc build. It’s definitely related to CCBReader.

That’s really strange, could you try the approach here (https://github.com/cocos2d/CCBReader/commit/a205146d4664e15f5fcf9d47146b26f016d6a055#commitcomment-2574544) ?

Hi guys, Thanks for your replies.

@James Chen - I did replace the code inside default case inside the readFloat function and did resubmit to apple but unfortunately it is still crashing on readFloat. And it still got approved by apple and still crashes once the app starts. I am unsure how to debug this because I cant even repro this issue myself. I have even taken the release build and put it on my device and it runs without any problems. Does apple recompile my ipa file?

Apple does not recompile your ipa but they are encrypting binary file in your ipa with their own certificate so your binary file inside ipa is not exactly the same as you send it to them. I’m not sure but I think that theoretically this could cause a crash after encrypting for example in case there are some memory related problems in your app like your app is writing some data to wrong pointer/memory address. I hope this is not happening.

edit:
As someone said here: https://github.com/cocos2d/CCBReader/commit/a205146d4664e15f5fcf9d47146b26f016d6a055#commitcomment-2693612 you could try disabling all compiler optimizations and resend it to the app store, however even if this works then it could only be treat like a temporary solution.

Hi,
Yeah so I will be resubmiting to the app store again without any compiler optimizations. I hope apple will expedite my review again. Looking around on google to see if anyone had a similar issue to this and I did find someone that was still setting up their compiler optimizations to -Os (fastest, smallest) but they wrap their code that they don’t want to be optimized. This might be a fix for the issue (not sure)

#pragma GCC push_options
#pragma GCC optimize ("O0")

CCBReader::readFloat() 
{
        ....
}

#pragma GCC pop_options

I will report back if the above worked.

Note that #pragma GCC push_options and #pragma GCC optimize (“O0”) probably works only if you use GCC compiler which in not default for XCode. Default iphone compiler is LLVM and this pragma directive probably won’t work so I suggest you to disable all optimizations. Besides even if this works with LLVM you can’t be sure that there are no other similar problems with CCBReader in other methods of this class. Also could you post here your code of readFloat method after you changed it and it also did not work?

Here is the crash log:

Incident Identifier: DCFEA2A4-9707-4FF7-A4B7-26CF61B4A73F
CrashReporter Key:   b91e2f134184158618753244f837ff2ac5114f0d
Hardware Model:      iPhone4,1
Process:         GAMEAPP [848]
Path:            /var/mobile/Applications/F860F02C-3D16-4995-A19F-CE036200209F/GAMEAPP.app/GAMEAPP
Identifier:      GAMEAPP
Version:         ??? (???)
Code Type:       ARM (Native)
Parent Process:  launchd [1]

Date/Time:       2013-03-01 19:42:44.987 -0500
OS Version:      iOS 6.1.2 (10B146)
Report Version:  104

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: EXC_ARM_DA_ALIGN at 0x1f0807c9
Crashed Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   GAMEAPP                     0x000f971c cocos2d::extension::CCBReader::readFloat() (CCBReader.cpp:422)
1   GAMEAPP                     0x000f9e52 cocos2d::extension::CCBReader::readKeyframe(int) (CCBReader.cpp:648)
2   GAMEAPP                     0x000f99ae cocos2d::extension::CCBReader::readNodeGraph(cocos2d::CCNode*) (CCBReader.cpp:509)
3   GAMEAPP                     0x000f9c20 cocos2d::extension::CCBReader::readNodeGraph(cocos2d::CCNode*) (CCBReader.cpp:586)
4   GAMEAPP                     0x000f9342 cocos2d::extension::CCBReader::readFileWithCleanUp(bool) (CCBReader.cpp:689)
5   GAMEAPP                     0x000f92ec cocos2d::extension::CCBReader::readNodeGraphFromData(cocos2d::extension::CCData*, cocos2d::CCObject*, cocos2d::CCSize const&, cocos2d::extension::CCBAnimationManager**) (CCBReader.cpp:230)
6   GAMEAPP                     0x000f91aa cocos2d::extension::CCBReader::readNodeGraphFromFile(char const*, cocos2d::CCObject*, cocos2d::CCSize const&, cocos2d::extension::CCBAnimationManager**) (CCBReader.cpp:213)
7   GAMEAPP                     0x000f90a4 cocos2d::extension::CCBReader::readNodeGraphFromFile(char const*, cocos2d::CCObject*) (CCBReader.cpp:192)
8   GAMEAPP                     0x00119b50 IntroScene::init() (IntroScene.cpp:55)
9   GAMEAPP                     0x00119ca8 IntroScene::create() (IntroScene.h:19)
10  GAMEAPP                     0x00119a7c IntroScene::scene() (IntroScene.cpp:22)
11  GAMEAPP                     0x00110290 AppDelegate::applicationDidFinishLaunching() (AppDelegate.cpp:144)
12  GAMEAPP                     0x000dab94 cocos2d::CCApplication::run() (CCApplication.mm:50)
13  GAMEAPP                     0x000b6610 -[AppController application:didFinishLaunchingWithOptions:] (AppController.mm:76)
14  UIKit                           0x36522ad4 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 248
15  UIKit                           0x3652265e -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1186
16  UIKit                           0x3651a846 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 694
17  UIKit                           0x364c2c34 -[UIApplication handleEvent:withNewEvent:] + 1000
18  UIKit                           0x364c26c8 -[UIApplication sendEvent:] + 68
19  UIKit                           0x364c2116 _UIApplicationHandleEvent + 6150
20  GraphicsServices                0x381d85a0 _PurpleEventCallback + 588
21  GraphicsServices                0x381d81ce PurpleEventCallback + 30
22  CoreFoundation                  0x3468f170 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 32
23  CoreFoundation                  0x3468f112 __CFRunLoopDoSource1 + 134
24  CoreFoundation                  0x3468df94 __CFRunLoopRun + 1380
25  CoreFoundation                  0x34600eb8 CFRunLoopRunSpecific + 352
26  CoreFoundation                  0x34600d44 CFRunLoopRunInMode + 100
27  UIKit                           0x36519480 -[UIApplication _run] + 664
28  UIKit                           0x365162fc UIApplicationMain + 1116
29  GAMEAPP                     0x000b6c22 main (main.m:14)
30  GAMEAPP                     0x000b6344 start + 36

Thread 1:
0   libsystem_kernel.dylib          0x3c8d5d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3c823cf6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3c823a12 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3c8238a0 start_wqthread + 4

Thread 2 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 2:
0   libsystem_kernel.dylib          0x3c8c5648 kevent64 + 24
1   libdispatch.dylib               0x3c7f5974 _dispatch_mgr_invoke + 792
2   libdispatch.dylib               0x3c7f5654 _dispatch_mgr_thread$VARIANT$mp + 32

Thread 3:
0   libsystem_kernel.dylib          0x3c8d5d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3c823cf6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3c823a12 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3c8238a0 start_wqthread + 4

Thread 4 name:  WebThread
Thread 4:
0   libsystem_kernel.dylib          0x3c8c4eb4 mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3c8c5048 mach_msg + 36
2   CoreFoundation                  0x3468f040 __CFRunLoopServiceMachPort + 124
3   CoreFoundation                  0x3468dd9e __CFRunLoopRun + 878
4   CoreFoundation                  0x34600eb8 CFRunLoopRunSpecific + 352
5   CoreFoundation                  0x34600d44 CFRunLoopRunInMode + 100
6   WebCore                         0x3a60a500 RunWebThread(void*) + 440
7   libsystem_c.dylib               0x3c82e30e _pthread_start + 306
8   libsystem_c.dylib               0x3c82e1d4 thread_start + 4

Thread 5 name:  AURemoteIO::IOThread
Thread 5:
0   libsystem_kernel.dylib          0x3c8c4eb4 mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3c8c5048 mach_msg + 36
2   AudioToolbox                    0x3418391c AURemoteIO::IOThread::Run() + 104
3   AudioToolbox                    0x34185c84 AURemoteIO::IOThread::Entry(void*) + 4
4   AudioToolbox                    0x340c3882 CAPThread::Entry(CAPThread*) + 294
5   libsystem_c.dylib               0x3c82e30e _pthread_start + 306
6   libsystem_c.dylib               0x3c82e1d4 thread_start + 4

Thread 0 crashed with ARM Thread State (32-bit):
    r0: 0x1ed49740    r1: 0x1f0807c9      r2: 0x000001cd      r3: 0x000001c8
    r4: 0x00000004    r5: 0x1ed49740      r6: 0x42480000      r7: 0x2fd4af48
    r8: 0xffffffff    r9: 0x000001c9     r10: 0x1ed49740     r11: 0x1ed4a8a0
    ip: 0x000001c7    sp: 0x2fd4af48      lr: 0x000f9e57      pc: 0x000f971c
  cpsr: 0x00000030

Binary Images:
..

And my readFloat() looks like this:

  float CCBReader::readFloat() {
        unsigned char type = this->readByte();

        switch (type) {
            case kCCBFloat0:
                return 0;    
            case kCCBFloat1:
                return 1;
            case kCCBFloatMinus1:
                return -1;
            case kCCBFloat05:
                return 0.5f;
            case kCCBFloatInteger:
                return (float)this->readInt(true);
            default:
                {
                    /* using a memcpy since the compiler isn't
                     * doing the float ptr math correctly on device.
                     * TODO still applies in C++ ? */
                    float * pF = (float*)(this->mBytes + this->mCurrentByte);
                    float f = 0;

                    memcpy((void*) &f, (const void*) pF, sizeof(float));

                    this->mCurrentByte += sizeof(float);


                    return f;
                }
        }
    }      

Looking at stackoverflow some people say that there is a problem with apple’s update system and they are working on a solution. Not sure if I believe it because I can seem to find any official documentation about it.

Hi,
Just wanted to report that apple has granted an expedited review for my app and the app got pushed threw and actually runs now. So to confirm this is with no optimizations (-Os). Now on the negative side of this is that the app runs noticeably slow on the iPhone 3GS. So I’ll have to come up with something to fix this. I am still wondering if anyone else had this issue before?

Good to know there is at least a workaround for this problem that works. Still it will be hard to find other solution if you can’t reproduce the problem without sending it to the app store. I’m wondering if compiler is still not applying some weird optimizations in this line: memcpy((void*) &f, (const void*) pF, sizeof(float)); even if you cast the pointers to void. You could try to compare disassembled code of your readFloat after adding cast to (void*) with original code to see if it is really different after adding that (void*) (there should be somewhere an option in xcode for disassembled code). Also look here: https://github.com/cocos2d/CCBReader/blob/master/CCBReader/CCBReader.m in original cocos2d they just changed memcpy to for loop and thay claimed that this resolved the crash problem.

Hey guys,
Try to use the way mentioned in http://www.cocos2d-x.org/boards/6/topics/18183?r=23985 .