This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Problems with k_free()

Hi,

I can run the following code within main.c successfully, but it produces the error as shown in the attachment upon execution of k_free(mem.ptr)  from with aggregator.c. Any ideas why it is not working?

   char *mem_ptr;

   mem_ptr = k_malloc(200);

   if (mem_ptr != NULL) {

       memset(mem_ptr, 0, 200);

       printk("Memory allocated:");

      }

      else

      {

        printk("Memory not allocated:\n");

      }

    k_free(mem_ptr);

    printk("Memory released:\n");

  • Hi,

     

    Is that the only change you did?

    According to the release notes for zephyr v2.3, this falls back to the "old" allocation:

    https://github.com/zephyrproject-rtos/zephyr/pull/25882/files#diff-d5ddd504431bec7f3b9e3d5ab926ec34

     

    Are you using dynamic memory allocation very actively (ie: using a lot of mem)? What happens if you set the heap to 24k for instance?

    What about your CONFIG_MAIN_STACK_SIZE (or the thread where k_free() is called from)? Since the pointer itself is stacked, it could be an issue there. What is your current main stack size?

     

    Kind regards,

    Håkon

  • CONFIG_MAIN_STACK_SIZE=8192

    Yes the following was the was the only change:

    CONFIG_MEM_POOL_HEAP_BACKEND=n

    When reading the following in kernel.h it almost seems like there is a more "loose" API on top of the heap.

    * @note When CONFIG_MEM_POOL_HEAP_BACKEND is enabled, the k_mem_pool
    * API is implemented on top of a k_heap, which is a more general
    * purpose allocator which does not make the same promises about
    * splitting or alignment detailed above.

    I don't think we are using a lot of memory, as we are filling a buffer with a few bytes then clearing the buffer when we have issued MQTT statements.

    But we are doing this continuously, and in saying that, the problem doesn't develop like a memory leak would, it happens immediately on the first call to K_free

    I wish I could offer more insight into the problem.

  • Hi,

     

    I am glad to hear that you have a workaround, but it is a bit concerning that this happens in the first place.

    I'd be happy to help debug, if you have a simple project that replicates the behavior.

     

    John said:
    I don't think we are using a lot of memory, as we are filling a buffer with a few bytes then clearing the buffer when we have issued MQTT statements.

    Some heap implementations splits into different chunk-sizes, like having x 32 byte chunks, y 128 byte chunks, and so-forth. If you alloc several smaller chunks, it might take up all the blocks that it normally fits into, and start taking up the next chunk size. However; this shouldn't explain the fault on free() call. If the pointer mem_ptr was corrupted somehow, it might cause an unaligned access or out-of-bounds access (both scenarios ends in a fault). I cannot say if that is the case here, as the failure looks very deterministic.

     

    Kind regards,

    Håkon

  • Please advise how I may send you the project by private message. (Commercially sensitive)

    I have a suspicion that it's the way that we are forming the project under v1.3 rather than something that is native to zephyr, which is why I'm keen for you to take a look. Because if we have done something stupid it would be great to understand what it was.

    Another issue that has become apparent is that we seem to have lost uart_2 connectivity between nrf52840 and the 9160 which worked fine inside v1.2

  • A private case is preferred if you need to share sensitive data. Since you're not the original poster in this case, you will lose access to the case if I made it private; so this scenario falls a bit between two stones.

    What you can do in the future is to input a private case, then share it with colleagues (Note: shared users only have read rights, not write)

     

    Kind regards,

    Håkon

Related