glibc mq_notify memory leak report

Parthiban parthiban@linumiz.com
Mon Jan 9 15:13:13 GMT 2023


Dear All,

I have the following code snippet and `mq_close`, `mq_unlink` is called in exit path.
But with valgrind, I have the summary about possibly lost.

How to gracefully remove the notification during exit path?

>From man mq_close,
If  the  calling process has attached a notification request (see (mq_notify(3)) to this
message queue via mqdes, then this request is removed

Valgrind summary:
```
==381555== HEAP SUMMARY:
==381555==     in use at exit: 5,716 bytes in 13 blocks                                                                                                       
==381555==   total heap usage: 39,786 allocs, 39,773 frees, 267,728,707 bytes allocated
==381555==                                                                                                                                                    
==381555== Thread 1:
==381555== 304 bytes in 1 blocks are possibly lost in loss record 3 of 5      
==381555==    at 0x4846A73: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==381555==    by 0x4011282: calloc (rtld-malloc.h:44)                                                                                                         
==381555==    by 0x4011282: allocate_dtv (dl-tls.c:375)                       
==381555==    by 0x4011C91: _dl_allocate_tls (dl-tls.c:634)
==381555==    by 0x4F244DF: allocate_stack (allocatestack.c:423)
==381555==    by 0x4F244DF: pthread_create@@GLIBC_2.34 (pthread_create.c:650)
==381555==    by 0x4F2E70E: init_mq_netlink (mq_notify.c:183)                
==381555==    by 0x4F28886: __pthread_once_slow (pthread_once.c:116)         
==381555==    by 0x4F2E802: __mq_notify (mq_notify.c:219)           
==381555==    by 0x4F2E802: mq_notify@@GLIBC_2.34 (mq_notify.c:203) 
==381555==    by 0x10C3D1: mq_notify_init (upstream.c:201)         
==381555==    by 0x10E306: mq_handler_init (upstream.c:1019)       
==381555==    by 0x10EDB6: upstream_init_async (upstream.c:1244)
==381555==    by 0x10BFD6: main (main.c:83)                     
==381555==                                                                     
==381555== LEAK SUMMARY:                                                      
==381555==    definitely lost: 0 bytes in 0 blocks                            
==381555==    indirectly lost: 0 bytes in 0 blocks                            
==381555==      possibly lost: 304 bytes in 1 blocks
==381555==    still reachable: 5,412 bytes in 12 blocks
==381555==         suppressed: 0 bytes in 0 blocks     
```

```
static int mq_notify_init(void)
{
        int ret;
        struct sigevent sev;

        sev.sigev_notify = SIGEV_THREAD;
        sev.sigev_notify_function = mq_handler;
        sev.sigev_notify_attributes = NULL;
        sev.sigev_value.sival_ptr = &mq;

        ret = mq_notify(mq, &sev);
        if (ret != 0)
                error("MQ notify register failed: %s", strerror(errno));

        return ret;
}
```
-- 
Thanks,
Parthiban N
https://www.linumiz.com
Berlin, Germany


More information about the Libc-help mailing list