f*ing pointer! Why doesn't this work?
Scott Lanning
slanning at theworld.com
Fri Jun 7 15:33:21 EDT 2002
On Thu, 6 Jun 2002, Frank Ramsay wrote:
> int threadSpawn(void (*func)(), int *aArg) {
> int idx;
> void *arg2;
> struct spawnData_s *data;
>
>
> if (threadPoolStarted == 0) {
> startThreadPool();
> }
>
> data = (struct spawnData_s *)malloc(sizeof(struct spawnData_s));
> data->func = func;
> data->arg = aArg;
> arg2 = data;
>
> printf("data %p - %p\n",&data->func, data->func);
>
> // look for an available slot to put the thread in
> for (idx = 0; idx < MAXTHREADS; idx++)
> if (ThreadPool[idx].Status == tp_Available) {
> pthread_create(&ThreadPool[idx].aThread, NULL, (void *)funcWrapper,
> &data);
Here you pass a pointer to a pointer (&data) rather than
the pointer itself.
[snip]
> void funcWrapper(void *obj) {
> struct spawnData_s *data;
> int idx;
> pthread_t me = pthread_self();
>
> // find self in the thread pool
> for (idx = 0; idx < MAXTHREADS; idx++)
> if (pthread_equal(ThreadPool[idx].aThread,me) != 0) {
> ThreadPool[idx].Status = tp_InUse;
> }
>
> data = (struct spawnData_s *)obj;
>
> printf("fpntr %p - %p\n",&data->func,data->func);
>
> // (data->func)(data->arg);
> free(data);
Thus, free tries to free the address of data,
so it segfaults.
More information about the Discuss
mailing list