2

I need to check for a memory leak in an embedded system.

The IDE is HEW and we are using uCOSIII RTOS.

Valgrind does not support the above configurations. Can you please suggest a tool or a method to check for memory leaks?

Martin Thompson
  • 16,395
  • 1
  • 38
  • 56
user2086002
  • 41
  • 1
  • 5

4 Answers4

6

First rule of dynamically allocating memory in embedded systems is "don't". Allocate it all once at the start of execution and then leave well alone. Otherwise you have to assess and decide what to do when a malloc (or similar operation) fails.

If you must dynamically allocate memory at runtime, then at its simplest you may be able to use a logging infrastructure to track calls to malloc/free by writing wrappers around them. Then you can track where and when the allocations and deallocations are happening and hopefully see what is missing.

Martin Thompson
  • 16,395
  • 1
  • 38
  • 56
  • 3
    In addition to handling failed malloc, you must also consider the fact that malloc is not a deterministic operation. In other words the amount of time a malloc takes may change from one call to the next and as such may cause you to miss real time deadlines... – Chris Desjardins Oct 23 '13 at 10:39
1

Take a look at libtalloc, the core memory allocator used in Samba. It may not work out-of-the-box for you if you don't have atexit() or stdio.h, but it shouldn't take too much work to port it to your environment.

Have a look at talloc_enable_leak_report_full() and talloc_report_full() (among others) to get you started.

Ben
  • 1,339
  • 1
  • 11
  • 15
1

I have been giving some thoughts about it, and here is a random try on how to do this with embedded systems:

  • First you need to check in which thread leakage occur. When doing alloc, you should also count for each thread how many active allocation. Where number of allocation keeps growing without deallocation, this is suspicious task
  • Secondly, you need to count number of allocations for allocs comming from that thread. To do this, replace alloc with a macro. Using macro you can save name of the file and line number where the call originated.

for example

#define alloc(x) my_alloc(x, __LINE__, __FILE__)

void * my_alloc(size_t size, int line, char * file)
{
     // increase number of allocations and dealocations for each combination line/file
}

Similarly you need to define my_free.

After this, run the program and printf from time to time allocations that keep growing. This should help find memory leaks.

P.S. I didn't test this, but I saw somebody do something similar in our code :)

Martin Thompson
  • 16,395
  • 1
  • 38
  • 56
Bogi
  • 2,274
  • 5
  • 26
  • 34
0

Your requirement is not completely clear. If you are looking for the tool as "valgrind" that can be able find the memory leak in your environment; that is difficult to find out.

If you are having some code than you can check all the memory allocations & freeing of the memory in the particular application. As link1 Link2

Also there are some files available by executing them you can find the memory leak.

http://code.axter.com/debugalloc.cpp
http://code.axter.com/debugalloc.h

http://code.axter.com/debuglogger.cpp
http://code.axter.com/debuglogger.h

http://code.axter.com/debuglog.c
http://code.axter.com/debuglog.h
  1. debugalloc.* code has the ability to track memory leaks, and it has description and usage information in comments.

  2. debuglogger.* code has some code for profileing your code.

  3. debuglog.* is some limited C version of the code.

Community
  • 1
  • 1
suneet saini
  • 592
  • 3
  • 16