Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for memory leaks in a C application on ARM Architecture

I am coding in C for a device that is using ARM architecture. I want to check for memory leaks in my application. As in my device in which iam working (ARM architecture ) does not support Valgrind .. so what other options I can use for checking for memory leaks in my application. Is there any way to check for memory leaks ... Or please guide what precautions I have to take while writing code so as to avoid memory leaks especially while dealing with strings and dynamic memory allocations ..?

Platform: Linux, gcc compiler

like image 790
john Avatar asked Nov 18 '11 11:11

john


2 Answers

Valgrind actually does support ARM nowdays (it even supports NEON SIMD instructions). If running debian or ubuntu, you should be able to install valgrind via apt. You may have to enable testing/unstable repositories, but the package exists.

Or you could of course compile it yourself (I have tried and it works).

like image 88
Leo Avatar answered Sep 27 '22 21:09

Leo


Don't use dynamic memory allocation. At least we don't in Avionics systems.

I typically use either

  1. malloc the required memory at initialization only.
    If it fails then the more memory is required for the application. This is used in a case where the driver needs to track N objects but N needs to be configurable per project / application. On the OS's I use a max memory value is provided to the OS for the application.

  2. Use a linked list of free and used items This works best if the items are of fixed size. Then at initialization the driver reads a configuration items that determines the max items it will support. This could be malloc'd from kernel space. If there is not enough memory the system resources needs to be specified correctly.

  3. Use a memory pool that the application can allocate from but delete only as a whole. In embedded OpenGL systems for avionics we allow applications to be able to create objects of variable size. Once the pool is exhausted we return an OUT_OF_MEMORY error. We do NOT allow the application to randomly delete the objects as this would cause memory fragmentation and non-deterministic run time behavior. We do allow them to delete every object and recreate them as required. This has deterministic behavior.

Everything has some sort of constraint or limit you need to determine based on the system's needs. That also applies for string data.

like image 44
fdk1342 Avatar answered Sep 27 '22 22:09

fdk1342