#include <poll.h>
 #include <limits.h>
 #include <err.h>
+#include <linux/list.h>
 #include <linux/time64.h>
 
 #define DATASIZE 100
 static unsigned int nr_loops = 100;
 static bool thread_mode = false;
 static unsigned int num_groups = 10;
+static struct list_head sender_contexts = LIST_HEAD_INIT(sender_contexts);
+static struct list_head receiver_contexts = LIST_HEAD_INIT(receiver_contexts);
 
 struct sender_context {
+       struct list_head list;
        unsigned int num_fds;
        int ready_out;
        int wakefd;
 };
 
 struct receiver_context {
+       struct list_head list;
        unsigned int num_packets;
        int in_fds[2];
        int ready_out;
        if (ret != 0)
                err(EXIT_FAILURE, "pthread_create failed");
 
+       pthread_attr_destroy(&attr);
        return childid;
 }
 
        if (!snd_ctx)
                err(EXIT_FAILURE, "malloc()");
 
+       list_add(&snd_ctx->list, &sender_contexts);
        for (i = 0; i < num_fds; i++) {
                int fds[2];
                struct receiver_context *ctx = malloc(sizeof(*ctx));
                if (!ctx)
                        err(EXIT_FAILURE, "malloc()");
 
+               list_add(&ctx->list, &receiver_contexts);
 
                /* Create the pipe between client and server */
                fdpair(fds);
        int readyfds[2], wakefds[2];
        char dummy;
        pthread_t *pth_tab;
+       struct sender_context *pos, *n;
 
        argc = parse_options(argc, argv, options,
                             bench_sched_message_usage, 0);
        }
 
        free(pth_tab);
-
+       list_for_each_entry_safe(pos, n, &sender_contexts, list) {
+               list_del_init(&pos->list);
+               free(pos);
+       }
+       list_for_each_entry_safe(pos, n, &receiver_contexts, list) {
+               list_del_init(&pos->list);
+               free(pos);
+       }
        return 0;
 }