Search code examples
network-programmingdynamic-memory-allocationcontiki

MEMB memory allocation in Contiki


In Contiki I declared a linked list like this:

MEMB(recv_memb, struct record, MAX_RECORD);
LIST(recv_list);

After using the linked list I free the allocated memory like this:

for(n = list_head(recv_list); n != NULL; n = n->next)
{     
    list_remove(recv_list,n); 
    memb_free(&recv_memb,n); 
}

but after reaching the MAX_RECORD the node restarts, How do I free the memory for another 30 records?

I'm simulating a networking scenario in Contiki Cooja simulator and the code is a combination of Contiki programming and C.


Solution

  • One problem (not sure if this will solve your issue completely...) is that you access the n->next pointer in the statement for(n = list_head(recv_list); n != NULL; n = n->next) after the element is already removed from the list and it's memory freed.

    Try this instead:

    void *next;
    for(n = list_head(recv_list); n != NULL; n = next)
    {     
        next = n->next;
        list_remove(recv_list,n); 
        memb_free(&recv_memb,n); 
    }