c++ - Need Help Tracking Down EXC_BAD_ACCESS on Function Entry on MacOS -
i have program gets kern_protection_failure exc_bad_access in strange place when running multithreaded , haven't faintest idea how troubleshoot further. on macos 10.6 using gcc.
the strange place gets when entering function. not on first line of function, actual jump function getmachinefactors():
program received signal exc_bad_access, not access memory. reason: kern_protection_failure @ address: 0xb00009ec [switching process 28242] 0x00012592 in getmachinefactors () @ ../sysinfo/osx.cpp:168 168 machinefactors* getmachinefactors() (gdb) bt #0 0x00012592 in getmachinefactors () @ ../sysinfo/osx.cpp:168 #1 0x000156d0 in collectmachinefactorsthreadproc (parameter=0x200280) @ threads.cpp:341 #2 0x952f681d in _pthread_start () #3 0x952f66a2 in thread_start () (gdb)
if run non-threaded, runs great, no issues:
#include "machinefactors.h" int main( int argc, char** argv ) { machinefactors* factors = getmachinefactors(); std::string str = createjsonobject(factors); cout << str; delete factors; return 0; }
if run in pthread, exc_bad_access above.
thread_function collectmachinefactorsthreadproc( lpvoid parameter ) { main* client = (main*) parameter; if ( parameter == null ) { errorlog( "no data passed machine identification thread. aborting." ); return 0; } machinefactors* mfactors = getmachinefactors(); // dies. // if don't call getmachinefactors , mfactors = // new machinefactors(); , threads communicate , exit // normally. if (mfactors == null) { errorlog("failed collect machine identification: getmachinefactors returned null." << endl) return 0; } client->machinefactors = createjsonobject(mfactors); delete mfactors; event_raise(client->machinefactorsevent); return 0; }
here excerpt getmachinefactors() code:
machinefactors* getmachinefactors() // dies on line in multi-threaded. { // printf( "getting machine factors.\n"); // tried , without this, never prints. factors = new machinefactors(); factors->osname = "macos"; factors->manufacturer = "apple"; ///… // gather various machine metrics here. //… return factors; }
for reference, using socketpair wait on thread complete:
// header file use cross-platform defines (this runs on osx, windows, , linux. struct _waitt { int fds[2]; }; #define thread_function void* #define thread_reference pthread_t #define mutex_reference pthread_mutex_t* #define mutex_lock(m) pthread_mutex_lock(m) #define mutex_unlock pthread_mutex_unlock #define event_reference struct _waitt #define event_wait(m) { char lc; if (read(m.fds[0], &lc, 1)) {} } while (0) #define event_raise(m) { char lc = 'j'; if (write(m.fds[1], &lc, 1)) {} } while (0) #define event_null(m) { m.fds[0] = -1; m.fds[1] = -1; } while (0)
here code launch thread.
void main::collectmachinefactors() { #ifdef win32 machinefactorsthread = createthread(null, 0, collectmachinefactorsthreadproc, this, 0, 0); if ( machinefactorsthread == null ) { errorlog( "could not create thread machine id: " << error_no << endl ) } #else int retval = pthread_create(&machinefactorsthread, null, collectmachinefactorsthreadproc, this); if (retval) { errorlog( "return code machine id pthread_create() " << retval << endl ) } #endif }
here's simple failure case of running multithreaded. fails code stack trace above:
collectmachinefactors(); event_wait(machinefactorsevent); cout << machinefactors; return 0;
at first suspected library problem. here's makefile:
# main executable file program = sysinfo # object files objects = version.h main.o protocol.o socket.o sslconnection.o stats.o timeelapsed.o formatter.o osx.o threads.o # include directories include = -itaocrypt/include -iyassl/taocrypt/mystl -iyassl/include -isysroot /developer/sdks/macosx10.5.sdk -mmacosx-version-min=10.5 # library settings staticlibs = libtaocrypt.a libyassl.a -wl,-rpath,. -ldl -lpthread -lz -lexpat # compile settings relcxx = g++ -g -ggdb -ddebug -wall $(include) .suffixes: .o .cpp .cpp.o : $(relcxx) -c -wall $(include) -o $@ $< all: $(program) $(program): $(objects) $(relcxx) -o $(program) $(objects) $(staticlibs) clean: rm -f *.o $(program)
i can't life of me see particularly odd or dangerous , i'm not sure look. same threaded process works fine on linux machine have tried. suggestions? tools should try?
i can add more info if helpful.
i can see problem windows code, not osx code that's crashing on you.
it seems you're not posting actual code getmachinefactors
, since variable factors
not declared. regarding debugging, should not take non-appearance of printf
output conclusive that statement hasn't been executed. use debugger facilities such setting breakpoint, using special debugger trace output, on (not sure gdb handles, it's primitive debugger, perhaps apple has better tools?).
for windows, should use run time library's thread creation instead of windows api createthread
. that's because createthread
runtime lib isn't informed. e.g, new
expression or other call uses runtime lib might fail.
sorry can't more.
i think perhaps have getmachinefactors
code haven't shown?
Comments
Post a Comment