labapart / gattlib

Library to access GATT information from BLE (Bluetooth Low Energy) devices
http://labapart.com/
436 stars 157 forks source link

There is a memory leak when the connection fails #246

Closed 13037639065 closed 2 years ago

13037639065 commented 2 years ago

test code

int main(int argc, const char *argv[]) {
    auto conn = gattlib_connect(NULL,"AA:AA:AA:AA:AA:AA", GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT);
    if(conn != NULL)
    {
        gattlib_disconnect(conn);
    }
    return 0;
}

valgrind result

Device 'AA:AA:AA:AA:AA:AA' cannot be found
==57642== 
==57642== HEAP SUMMARY:
==57642==     in use at exit: 156,086 bytes in 1,844 blocks
==57642==   total heap usage: 6,740 allocs, 4,896 frees, 371,638 bytes allocated
==57642== 
==57642== LEAK SUMMARY:
==57642==    definitely lost: 40 bytes in 1 blocks
==57642==    indirectly lost: 5 bytes in 1 blocks
==57642==      possibly lost: 2,688 bytes in 27 blocks
==57642==    still reachable: 131,561 bytes in 1,652 blocks
==57642==                       of which reachable via heuristic:
==57642==                         length64           : 1,168 bytes in 19 blocks
==57642==                         newarray           : 1,744 bytes in 29 blocks
==57642==         suppressed: 0 bytes in 0 blocks
==57642== Rerun with --leak-check=full to see details of leaked memory
==57642== 
==57642== For lists of detected and suppressed errors, rerun with: -s
==57642== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

fix comments. Add the following code to the end of function gattlib_connect

    // destroy default adapter
    if(adapter == NULL)
    {
        gattlib_adapter_close(gattlib_adapter);
    }