• Guilherme Maciel Ferreira's avatar
    Fix memory leak on Socket_new() · af53c72a
    Guilherme Maciel Ferreira authored
    There is a memory leak on Socket_new(). The leak is caused by the lost of a
    reference to result pointer, returned from getaddrinfo(3). The freeaddrinfo(3)
    requires a pointer to the first result's element. However, this reference is
    lost when replaced by res.
    
    The Valgrind's memory leak report:
    
      ==15305== HEAP SUMMARY:
      ==15305==     in use at exit: 60 bytes in 1 blocks
      ==15305==   total heap usage: 205 allocs, 204 frees, 128,834 bytes allocated
      ==15305==
      ==15305== 60 bytes in 1 blocks are definitely lost in loss record 1 of 1
      ==15305==    at 0x4024F20: malloc (vg_replace_malloc.c:236)
      ==15305==    by 0x419650F: gaih_inet (getaddrinfo.c:1253)
      ==15305==    by 0x41994F4: getaddrinfo (getaddrinfo.c:2425)
      ==15305==    by 0x4036D3A: Socket_new (Socket.c:607)
      ==15305==    by 0x40352A4: MQTTProtocol_connect (MQTTProtocolOut.c:97)
      ==15305==    by 0x402FF71: MQTTClient_connectURIVersion (MQTTClient.c:792)
      ==15305==    by 0x403033F: MQTTClient_connectURI (MQTTClient.c:1025)
      ==15305==    by 0x4030496: MQTTClient_connect (MQTTClient.c:1086)
      ==15305==    by 0x8048A31: myconnect (stdoutsub.c:98)
      ==15305==    by 0x8048C73: main (stdoutsub.c:146)
      ==15305==
      ==15305== LEAK SUMMARY:
      ==15305==    definitely lost: 60 bytes in 1 blocks
      ==15305==    indirectly lost: 0 bytes in 0 blocks
      ==15305==      possibly lost: 0 bytes in 0 blocks
      ==15305==    still reachable: 0 bytes in 0 blocks
      ==15305==         suppressed: 0 bytes in 0 blocks
      ==15305==
      ==15305== For counts of detected and suppressed errors, rerun with: -v
      ==15305== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 19 from 10)
    
    Keeping the result pointer unchanged, pointing to the first element. And
    updating the res pointer only, we get a clean report from Valgrind:
    
      ==15924== HEAP SUMMARY:
      ==15924==     in use at exit: 0 bytes in 0 blocks
      ==15924==   total heap usage: 334 allocs, 334 frees, 131,753 bytes allocated
      ==15924==
      ==15924== All heap blocks were freed -- no leaks are possible
      ==15924==
      ==15924== For counts of detected and suppressed errors, rerun with: -v
      ==15924== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 10)
    Signed-off-by: 's avatarGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
    af53c72a
Name
Last commit
Last update
..
samples Loading commit data...
Clients.c Loading commit data...
Clients.h Loading commit data...
Heap.c Loading commit data...
Heap.h Loading commit data...
LinkedList.c Loading commit data...
LinkedList.h Loading commit data...
Log.c Loading commit data...
Log.h Loading commit data...
MQTTAsync.c Loading commit data...
MQTTAsync.h Loading commit data...
MQTTClient.c Loading commit data...
MQTTClient.h Loading commit data...
MQTTClientPersistence.h Loading commit data...
MQTTPacket.c Loading commit data...
MQTTPacket.h Loading commit data...
MQTTPacketOut.c Loading commit data...
MQTTPacketOut.h Loading commit data...
MQTTPersistence.c Loading commit data...
MQTTPersistence.h Loading commit data...
MQTTPersistenceDefault.c Loading commit data...
MQTTPersistenceDefault.h Loading commit data...
MQTTProtocol.h Loading commit data...
MQTTProtocolClient.c Loading commit data...
MQTTProtocolClient.h Loading commit data...
MQTTProtocolOut.c Loading commit data...
MQTTProtocolOut.h Loading commit data...
MQTTVersion.c Loading commit data...
Messages.c Loading commit data...
Messages.h Loading commit data...
SSLSocket.c Loading commit data...
SSLSocket.h Loading commit data...
Socket.c Loading commit data...
Socket.h Loading commit data...
SocketBuffer.c Loading commit data...
SocketBuffer.h Loading commit data...
StackTrace.c Loading commit data...
StackTrace.h Loading commit data...
Thread.c Loading commit data...
Thread.h Loading commit data...
Tree.c Loading commit data...
Tree.h Loading commit data...
utf-8.c Loading commit data...
utf-8.h Loading commit data...