memory leak with getaddrinfo_a

Hi there!

I got a memory leak using the call 'getaddrinfo_a'. This is an example code taken from the Linux man pages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int i, ret;
    struct gaicb * reqs[argc - 1];
    char host[NI_MAXHOST];
    struct addrinfo *res;

    if (argc < 2)
    {
        fprintf(stderr, "Usage: %s HOST...\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    for (i = 0; i < argc - 1; i++)
    {
        reqs[i] = (gaicb*)malloc(sizeof (*reqs[0]));
        if (reqs[i] == NULL)
        {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
        memset(reqs[i], 0, sizeof (*reqs[0]));
        reqs[i]->ar_name = argv[i + 1];
    }

    ret = getaddrinfo_a(GAI_WAIT, reqs, argc - 1, NULL);
    if (ret != 0)
    {
        fprintf(stderr, "getaddrinfo_a() failed: %s\n",
                gai_strerror(ret));
        exit(EXIT_FAILURE);
    }

    for (i = 0; i < argc - 1; i++)
    {
        printf("%s: ", reqs[i]->ar_name);
        ret = gai_error(reqs[i]);
        if (ret == 0)
        {
            res = reqs[i]->ar_result;

            ret = getnameinfo(res->ai_addr, res->ai_addrlen,
                    host, sizeof (host),
                    NULL, 0, NI_NUMERICHOST);
            if (ret != 0)
            {
                fprintf(stderr, "getnameinfo() failed: %s\n",
                        gai_strerror(ret));
                exit(EXIT_FAILURE);
            }
            puts(host);
            
            /*
             * Still got a memory leak with these lines
            freeaddrinfo(reqs[i]->ar_result);
            free (reqs[i]);
            */
        }
        else
        {
            puts(gai_strerror(ret));
        }
    }
    exit(EXIT_SUCCESS);
}


I added the commented lines, but still got the memory leak.

And these are the error messages showed by valgrind:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
==8866== 272 bytes in 1 blocks are possibly lost in loss record 5 of 6
==8866==    at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8866==    by 0x40138A4: allocate_dtv (dl-tls.c:322)
==8866==    by 0x40138A4: _dl_allocate_tls (dl-tls.c:539)
==8866==    by 0x541026E: allocate_stack (allocatestack.c:588)
==8866==    by 0x541026E: pthread_create@@GLIBC_2.2.5 (pthread_create.c:539)
==8866==    by 0x4E3B49A: __gai_create_helper_thread (gai_misc.h:113)
==8866==    by 0x4E3B49A: __gai_enqueue_request (gai_misc.c:256)
==8866==    by 0x4E3BBB4: getaddrinfo_a (getaddrinfo_a.c:67)
==8866==    by 0x400AE4: main (getaddrinfo_a_example.cpp:31)
==8866==
==8866== LEAK SUMMARY:
==8866==    definitely lost: 0 bytes in 0 blocks
==8866==    indirectly lost: 0 bytes in 0 blocks
==8866==      possibly lost: 272 bytes in 1 blocks
==8866==    still reachable: 2,588 bytes in 9 blocks
==8866==         suppressed: 0 bytes in 0 blocks
==8866== Reachable blocks (those to which a pointer was found) are not shown.
==8866== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==8866==
==8866== For counts of detected and suppressed errors, rerun with: -v
==8866== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)


Why am I having a memory leak with getaddrinfo_a?

Thanks a lot!
See the man page, you should be calling freeaddrinfo().
https://linux.die.net/man/3/getaddrinfo
there kind of errors are a result of high complexity, I;s recommend you to try and make things easier, as those errors can be hard to be detected, unless you use one of those programs, such as checkmarx. Make sure you detect and sole them all as they pretty much make you stop working in the middle.
Good luck.
Topic archived. No new replies allowed.