resource cleanup in cyclic calls to netlink

ferran ranker72 at gmail.com
Wed Sep 16 14:25:54 PDT 2015


Hi Surabhi,

I finally solved that. My issue was totally particular to my
environment. I already had an open socket and was confused about which I
was connecting to. The solution was to open the netlink socket to a
different port, because somehow, when trying to free a socket "sk"
binded to the default port, the application crashed as if I didn't own
that piece of memory.

Be sure to free your socket like this:

    sk = nl_socket_alloc();
    nl_socket_set_local_port(sk, YOUR_PORT);
    genl_connect(sk);
    /* use socket */
    nl_socket_free(sk);

Be sure also to free the socket even if you have some error and don't
get to talk to the kernel. You can look at the source code of "iw"
https://kernel.org/pub/software/network/iw/iw-4.1.tar.gz
The netlink communication cycle kind of starts at line 340 of "iw.c". Be
aware that iw performs just one command per execution, meaning that it
doesn't reuse resources as our applications.

Regarding the "36 bytes leftover" message, it looks like it's nla_parse
who warns it http://www.spinics.net/lists/netdev/msg193475.html
Maybe you have missed some parameters at "nla_policy" when doing
nla_parse_nested? It's just an idea, I recommend you to browse libnl
source code and go find the origin of that message.

Feel free to post some code so that we can understand when you are
allocating memory and when freeing it.

Best regards,
Ferran

On 16/09/15 10:04, Surabhi Goswami wrote:
> Hi, 
>
> I just came across your question regarding resource cleanup : 
>
> http://lists.infradead.org/pipermail/libnl/2015-June/001905.html 
>
> I am also facing a similar kind of issue, I am trying to port meshd-nl80211 on my embedded 
> system ,although the connection gets established but there is a constant memory leak due 
> to the nlmsg_alloc. 
>
> I tried to allocate a single msg globally and reuse it, but I encounter this print 
> "netlink: 36 bytes leftover after parsing attributes." continuously. 
>
> I wished to know if you were able to solve the memory leak issue. 
>
> Any help would be appreciated. 
>
> Thanks in advance! 




More information about the libnl mailing list