Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

f*ing pointer! Why doesn't this work?



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.





BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org