Locking a mutex crashes when caused by static initialization

771 views
Skip to first unread message

Peter Holtwick

unread,
Mar 22, 2011, 12:29:56 PM3/22/11
to android-ndk
The call of
"pthread_mutex_lock( &m_Mutex )"
crashes, although
"pthread_mutexattr_init( &m_MutexAttr )",
"pthread_mutexattr_settype (&m_MutexAttr, PTHREAD_MUTEX_RECURSIVE_NP)"
and
"pthread_mutex_init(&m_Mutex, &m_MutexAttr )"
are done successfully before.

It's a standalone executable running on the emulator with Android 1.5.
This is for debugging purposes, it's a shared library at the end, and
as this, it doesn't crash.

The crash happens at the very start, callstack coming from "global
constructors" and "__static_initialization_and_destruction_0()".

Does an executable behave different than a library in terms of
initialization of statics? Is it not ok to use a mutex that early?
Peter

alan

unread,
Mar 22, 2011, 1:24:31 PM3/22/11
to andro...@googlegroups.com
in general doing anything complex during static initialisation may cause you problems. have you tried initialising your mutex from jni_onload instead? this would be called after static initialisation is complete.

Peter Holtwick

unread,
Mar 23, 2011, 9:28:27 AM3/23/11
to android-ndk
The thing is that it's a standalone executable, without jni_onload.
And compiled as shared library the static initialization doesn't make
any trouble.

alan schrieb:

alan

unread,
Mar 23, 2011, 10:49:51 AM3/23/11
to andro...@googlegroups.com, Peter Holtwick
how about initialising in main instead?

David Turner

unread,
Mar 23, 2011, 1:19:14 PM3/23/11
to andro...@googlegroups.com, Peter Holtwick
There were several bugs in the dynamic linker related to static C++ initialization that were fixed later in the platform
(I think it was in 2.0). If you want to target 1.5 and 1.6, you probably don't want to use this feature at all.

Sorry :-(


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


Peter Holtwick

unread,
Mar 24, 2011, 6:44:24 AM3/24/11
to android-ndk
Thanks alan+David!

Unfortunately, the project is big and static variables are quite
popular. I'm not a fan of it, but it would be difficult to switch to a
structured initialization process at startup. You just cannot prevent
that somebody adds a global variable or static in his his code :(

Peter

fadden

unread,
Mar 25, 2011, 8:29:08 PM3/25/11
to android-ndk
In that case, you may need to specify a minimum Android API version
that prevents the code from being installed on older devices.
Reply all
Reply to author
Forward
0 new messages