Unexpected value-at for NULL'd pointer with pthreads

Jonathan Wakely jwakely.gcc@gmail.com
Mon Aug 24 15:23:00 GMT 2015


On 24 August 2015 at 16:17, Jonathan Wakely wrote:
> On 24 August 2015 at 15:29, Kyle Harper wrote:
>>
>> void starting_routine(List *list) {
>>   // Starting point a thread will use.
>>   Buffer *local_buf_ptr;
>>   printf("%d : Thread starting up.  local_buf_ptr is currently %d, ",
>> pthread_self(), local_buf_ptr);
>>   local_buf_ptr = list->pool[0];
>>   printf("and is now %d\n", local_buf_ptr);
>>   break_crap(list, &local_buf_ptr);
>
> Here you pass the address of a local variable.
>
>>   pthread_exit(0);
>> }
>>
>>
>> void break_crap(List *list, Buffer **buf) {
>>   // Emulate the buffer removal.
>>   pthread_mutex_lock(&list->lock);
>>   printf("%d : checking to see if *buf is null\n", pthread_self());
>>   if (*buf == NULL) {
>
> Here you check if that local variable is null.
>
>>     printf("%d : *buf is null so I'm leaving.\n", pthread_self());
>>     pthread_mutex_unlock(&list->lock);
>>     return;
>>   }
>>   printf("%d : *buf is not null and has ID=%d and lock_id=%d,
>> free()ing and NULL-ing\n", pthread_self(), (*buf)->id,
>> (*buf)->lock_id);
>>   free(*buf);
>>   *buf = NULL;
>
> You are nulling the local variable, not the array element it points to.

In case that doesn't make the problem obvious: each thread executing
start_routine has its own local variables.

You probably want the change shown here:

https://gist.github.com/jwakely/d8453e7f20256456c04d/revisions



More information about the Gcc-help mailing list