Search code examples
cmemory-managementmemory-leaksbluetoothbluez

Should I free memory after calling the sdp_list_append function?


I was reading the source code of the PyBluez library and noticed that in this block of code memory is allocated with malloc, but never freed:

 // add service classes, if any
for(i = 0; i < PySequence_Length(service_classes); i++) {
    uuid_t *svc_class_uuid = (uuid_t*) malloc( sizeof( uuid_t ) );
    PyObject *item = PySequence_GetItem(service_classes, i);
    pyunicode2uuid( item, svc_class_uuid );
    svc_class_list = sdp_list_append(svc_class_list, svc_class_uuid);
}
sdp_set_service_classes(&record, svc_class_list);

I assume that this is a memory leak if sdp_list_append() function does not frees the pointer but I could not find any source codes of this function to check.

Should I add free(svc_class_uuid); in the end of this block in my realization?


Solution

  • As I have understood the code appends dynamically built items svc_class_uuid to the list svc_class_list. So the allocated memory is freed when an item is popped from the list or when the list stops its existence or performs a clean up operation.

    As there is no pop operation in this code snippet then you shall not free the allocated memory yourself. It is operations on the list that responsible to free the allocated memory.