This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Reset Heap & malloc/free

Hi there,

Is there a way to re-initialize the whole heap region? It should also reset "internal pointer" of the malloc method. I use malloc/free in only one task and I want to start/stop this task. This should prevent memory leaks.

Chip: Cortex M3, LPC1788

Thx, Frederic

Parents Reply Children
  • At the moment I'm doing a reset of the device to make the free-all. It works fine, but it's not so "nice". Still I think I will stick to that solution at the moment.

    Since this isn't only my code, free() isn't used for each malloc(). Theoretically, if I manage to free() every malloc(), will the state be the same as after a re-initialization?

    Frederic

  • To answer your question: Yes. That the idea. Of course the RAM dedicated to the heap will not be 0, but it will be empty.
    Why not spend some time to find the root cause, i.e the memory leak?

  • Theoretically, if I manage to free() every malloc(), will the state be the same as after a re-initialization?

    The answer to that is it might. It totally depends upon the implementation of malloc() and free(). It might keep the internal list of previous allocations intact and therefore might still be fragmented, thus it might not be able to allocate a large block. We have certainly witnessed some heap allocation strategies that work in this manner.