Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 8 months ago.
How can i detect memory leak in my mbed application?
Hi
How can I detect whether any memory leaks are there in my application? Is there any tool to find unwanted memory usage. Please do help me
Thanks in advance
1 Answer
9 years, 8 months ago.
If you search on mbed there are some techniques to get an idea how much memory is free. Basic idea is to make a stack variable (so just int i = 0;) and a heap variable (int *j = new int;), and then check the different in address between the pointer to i, and j.
But in principle as David wrote: Don't make memory leaks. The only way you can get a memory leak is by calling malloc/new without using free/delete. So preferable option: Don't call malloc/new. Sometimes you need it, but really often it simply is not required. If you do need it, make sure that no matter which path your program takes, it always ends up free'ing the variable. Especially make sure if you use the same pointer again to malloc/new something, you beforehand always make sure the old one is deleted.
Memory leaks are normally due to sloppy programming. If you are using malloc etc, then make sure you free the memory after its been used, ie free(buffer);. You can lookup that method by going to www.cplusplus.com.
Dave.
posted by David Fletcher 19 Jan 2016