Showing posts with label iPhone. Show all posts
Showing posts with label iPhone. Show all posts

Thursday, March 08, 2012

iOS device: EXC_BAD_ACCESS error with EXC_ARM_DA_ALIGN code

I spent a day hunting down one nasty bug in my code.
This code worked fine on Android ARM7 build, iPhone simulator (i386) build,
but started to crash once I run it on iPhone 4 device.

The code was implementing simple wrappers for the mutex, the event and the thread API.

For example the mutex handle looked something like this:

typedef struct _MUTEX_HANDLE {
    .........
    pthread_mutex_t mutex;
} MUTEX_HANDLE;

Then the event and thread structures where built upon this.

The problem only surfaced on iOS device, with error EXC_BAD_ACCESS and the code EXC_ARM_DA_ALIGN. I checked and double checked all the structures in memory their alignment etc., and still could not see anything wrong.  I searched online and got distracted with many articles reporting the similar type of problem and suggesting memcpy as a solution in order to "fix" this alignment issue.

The definitions of the pthread handles though was kind of curious:
struct _opaque_pthread_mutex_t { long __sig; char __opaque[__PTHREAD_MUTEX_SIZE__]; };
in addition to the signal handle there was an array.

Eventually I narrowed this down to the simple assignment by value where I just initialized 
pthread_mutex_t variable and assigned it by value to my structure member.

static int createMutex(MUTEX_HANDLE **pHandle) {
    pthread_mutex_t mtx;
    int err = initMutex(&mtx);
    if (!err) { 
        MUTEX_HANDLE *ph = malloc(sizeof(MUTEX_HANDLE));
        if (ph) {
           ph->mutex = mtx; //the problem is here, never do this!!! 

The pthread_mutex_t type is very simple structure on Android with one int member, so I kind of assumed that assigning pthread_mutex_t by value would not incur that much overhead or side effects and just did what I did above. So, this was not the best decision on my part.

The fix was to eliminate the temporary pthread_mutex_t variable on a stack and use the mutex member of the MUTEX_HANDLE structure directly: 

static int createMutex(MUTEX_HANDLE **pHandle) {
    MUTEX_HANDLE *ph = malloc(sizeof(MUTEX_HANDLE));
    if (ph) {
        int err = initMutex(&ph->mutex);

So, the EXC_ARM_DA_ALIGN error was just a big distraction that had me wasting quite a bit of time looking in the wrong direction. 


iOS 5.1 and Xcode 4.3.1 upgrade

I got the iOS 5.1 upgrade for my iPhone yesterday.
Then the Xcode needed to be upgraded to 4.3.1 in order to work with my phone with iOS 5.1.
Then cpp compiler link got screwed up again.
In the previous post I mentioned that you can fix with creating the new link:
sudo ln -s llvm-cpp-4.2 cpp


The old directory /Developer is gone now and the new one where you have to create the link is:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/

Enjoy!

Thursday, November 17, 2011

if you are using sem_timedwait on iOS or Mac OS X ....

While porting some C/C++ code to iOS found out that sem_timedwait is not implemented on iOS or Mac OS X and seems like on many BSD systems. Here is the discussion on the topic:
http://stackoverflow.com/questions/641126/posix-semaphores-on-mac-os-x-sem-timedwait-alternative
The discussion a bit old but it seems like it is still a valid one.

For now I conditionally compiled the code with the call sem_wait, will look into a better cross-platform solution later.

Wednesday, November 16, 2011

Native libraries porting to iOS

Recently I spent a bit of time working on porting (getting built) some native libraries with Android NDK and iOS. The port to Android NDK deserves it's own post, that I might write down later.

As for iOS, I primarily needed openssl, curl, zlib and found nice little script, that helps you with the build:
http://code.google.com/p/ios-static-libraries. Just comment out what you don't need and you should be good to go. It's quite easy to repurpose the script to pull the source from your local repos if needed.

One of the problem I had was that I have only iOS SDK 5.0 on the machine and the libcurl build for iPhone device was breaking because "cpp" compiler (export CXXCPP=${DEVROOT}/usr/bin/cpp
) was not found . Apple provided "cpp" compiler for the emulator, but conveniently left it out from the iPhone device toolchain. The easy fix for that is to create the symbolic link in the /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin directory:
ln -s llvm-cpp-4.2 cpp

Hope this saves you a bit of time.