I built my own TaskQueue for my multi-threaded program but there is an error on the allocation of memory somewhere in the code.
In main.c:
<code>char *endpoint;
while ((endpoint = (char *)queueDequeue(queue)) != NULL){
CURL *handle = easyInit();
</code>
<code>char *endpoint;
while ((endpoint = (char *)queueDequeue(queue)) != NULL){
CURL *handle = easyInit();
</code>
char *endpoint;
while ((endpoint = (char *)queueDequeue(queue)) != NULL){
CURL *handle = easyInit();
In task_queue.h:
<code>void *queueDequeue(TaskQueue *queue) {
pthread_mutex_lock(&queue->lock);
if (queue->head == NULL) {
pthread_mutex_unlock(&queue->lock);
return NULL;
}
Task *temp = queue->head;
char *data = strdup(temp->endpoint);
queue->head = temp->next;
if (queue->head == NULL)
queue->tail = NULL;
free(temp);
pthread_mutex_unlock(&queue->lock);
return data;
}
</code>
<code>void *queueDequeue(TaskQueue *queue) {
pthread_mutex_lock(&queue->lock);
if (queue->head == NULL) {
pthread_mutex_unlock(&queue->lock);
return NULL;
}
Task *temp = queue->head;
char *data = strdup(temp->endpoint);
queue->head = temp->next;
if (queue->head == NULL)
queue->tail = NULL;
free(temp);
pthread_mutex_unlock(&queue->lock);
return data;
}
</code>
void *queueDequeue(TaskQueue *queue) {
pthread_mutex_lock(&queue->lock);
if (queue->head == NULL) {
pthread_mutex_unlock(&queue->lock);
return NULL;
}
Task *temp = queue->head;
char *data = strdup(temp->endpoint);
queue->head = temp->next;
if (queue->head == NULL)
queue->tail = NULL;
free(temp);
pthread_mutex_unlock(&queue->lock);
return data;
}
causes the following error:
<code>==62212== 440 bytes in 5 blocks are definitely lost in loss record 147 of 510
==62212== at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==62212== by 0x1204675E: make_call (main.c:316)
==62212== by 0x4A36AC2: start_thread (pthread_create.c:442)
==62212== by 0x4AC7A03: clone (clone.S:100)
</code>
<code>==62212== 440 bytes in 5 blocks are definitely lost in loss record 147 of 510
==62212== at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==62212== by 0x1204675E: make_call (main.c:316)
==62212== by 0x4A36AC2: start_thread (pthread_create.c:442)
==62212== by 0x4AC7A03: clone (clone.S:100)
</code>
==62212== 440 bytes in 5 blocks are definitely lost in loss record 147 of 510
==62212== at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==62212== by 0x1204675E: make_call (main.c:316)
==62212== by 0x4A36AC2: start_thread (pthread_create.c:442)
==62212== by 0x4AC7A03: clone (clone.S:100)
I originally thought it had something to do with the strdup
but, normally, if it was the memory allocated by it – it would say strdup
was the memory allocator. I may be wrong though. The leak is happening somewhere even though endpoint is properly freed upon completion of the tasks.
1