valgrind and pthreads (on os x El Capitan): can't make a smallest working example?

烈酒焚心 提交于 2019-12-11 00:26:20

问题


A pthreads program seems to work fine for me on El Capitan, but when I run with valgrind I get a SIGSEGV crash 'Access not within mapped region at address 0x700002F32C05' from within pthread_join.

The stacktrace differs slightly, based on whether I'm trying to access a return-value from the thread or not (ends in _pthread_find_thread vs _OSSpinLockLockSlow).

My question: Am I mis-understanding how to use pthreads, or is this a valgrind-on-Yosemite issue? (Or, something else?)

I'm running valgrind 3.11, gcc clang-700.1.76 ('thread model: posix'), OS X 10.11.3. (I see just now that valgrind says v3.11.0 is "preliminary support for El Capitan", so maybe that's the issue?)

==== version A ====
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>

void *myThread() {
  int* retPtr = malloc(sizeof(int));
  *retPtr = 42;
  return (void*) retPtr;
  }

int main() {
   pthread_t tid;
   int *answer;

   pthread_create(&tid, NULL, myThread, NULL);
   pthread_join(tid, (void**)&answer);

   printf("%d\n", *answer);
   free((void*)answer);
   return 0;
}

==== Version B (EDIT: now pasted correctly) ====

#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>

void *myThread() { 
    return NULL;
    }

int main() {
    pthread_t tid;

    pthread_create(&tid, NULL, myThread, NULL);
    pthread_join(tid, NULL);

    return 0;
    }

==== stack trace for version A: ====

==39520== Process terminating with default action of signal 11 (SIGSEGV)
==39520==  Access not within mapped region at address 0x700002F32C3E
==39520==    at 0x10044437F: _pthread_find_thread (in /usr/lib/system/libsystem_pthread.dylib)
==39520==    by 0x100446CE2: _pthread_lookup_thread (in /usr/lib/system/libsystem_pthread.dylib)
==39520==    by 0x100446ABA: pthread_join (in /usr/lib/system/libsystem_pthread.dylib)
==39520==    by 0x100000F27: main (hoo.c:15)

==== Stack trace for Version B: ====

==39528== Process terminating with default action of signal 11 (SIGSEGV)
==39528==  Access not within mapped region at address 0x700002F32C05
==39528==    at 0x100432E2B: _OSSpinLockLockSlow (in /usr/lib/system/libsystem_platform.dylib)
==39528==    by 0x100446B10: pthread_join (in /usr/lib/system/libsystem_pthread.dylib)
==39528==    by 0x100000F57: main (hoo.c:14)

来源:https://stackoverflow.com/questions/35964228/valgrind-and-pthreads-on-os-x-el-capitan-cant-make-a-smallest-working-exampl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!