Valgrind reporting memory leak with dynamic heap

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
cduran
Posts: 70
Joined: Thu Mar 17, 2016 4:52 pm

Valgrind reporting memory leak with dynamic heap

Post by cduran » Fri May 13, 2022 9:05 pm

I'm currently building MP as a library based off the minimal build linked into a linux application. I would like to use a dynamically allocated heap as follows

Code: Select all

//#define MP_USE_MALLOC_HEAP
#ifdef MP_USE_MALLOC_HEAP
static char* mp_heap;
#else
static char mp_heap[PYTHON_HEAP_SIZE];					// Micropython's heap
#endif
.
.
#ifdef MP_USE_MALLOC_HEAP
mp_heap = (char*)malloc(PYTHON_HEAP_SIZE);
#endif
.
.
mp_stack_ctrl_init();
gc_init(mp_heap, mp_heap + PYTHON_HEAP_SIZE);
mp_init();
When I test with Valgrind I'm getting the following results:

Built time heap
CaptureM.JPG
CaptureM.JPG (24.85 KiB) Viewed 70 times
Malloc runtime heap
CaptureNM.JPG
CaptureNM.JPG (23.89 KiB) Viewed 70 times

Anyone have any ideas as to why am I getting more memory leak from the dynamically allocated heap?

stijn
Posts: 722
Joined: Thu Apr 24, 2014 9:13 am

Re: Valgrind reporting memory leak with dynamic heap

Post by stijn » Sun May 15, 2022 4:20 pm

There's a malloc() for mp_heap but code shows no call to free(), hence it's a leak? That, or valgrind is incorrectly reporting the static memory as leaked.

Post Reply