Fault address when malloc/free pthread_t across threads
- by scleung
Fault address occurred when i malloc pthread_t to save a newly created thread id and free it in another thread. Codes as follows:
typedef struct _TaskInfo {
// int dummy_int;
pthread_t tid;
} TaskInfo;
void* dummy_task(void* pArg) {
free(pArg);
return NULL;
}
void create_task() {
TaskInfo *pInfo;
pthread_attr_t attr;
// set detached state stuff ...
pInfo = (TaskInfo*) malloc(sizeof(TaskInfo));
pthread_create(&pInfo->tid, &attr, dummy_task, pInfo);
// destroy pthread attribute stuff ...
}
int main() {
int i;
while(i < 10000) {
create_task();
++i;
}
return 0;
}
When I uncomment the member dummy_int of TaskInfo it sometimes ran successfully, but sometimes failed.
My platform is VMWare + Ubuntu 9.10 + ndk r3
Thanks!