Memory leak observed (regfree() not freeing total allocated memory) while using <regex.h>

Hi, I am using <regex.h> in my code and observing memory leak. After some debugging, I found out that regfree is not able to free all the memory allocated to.

Memory_Used -> 26560
regcomp (+320)
Memory_Used -> 26880
regex (+0)
Memory_Used-> 26880
free (-188)
Memory_Used -> 26692

total leak -> 188 KB

From the above data, we can see that when we call regcomp there 320 KB of data is getting allocated. But when I called regfree only part of memory is freed and 188KB remains.
Cool story bro.
Seriously, if you want people to pay attention, then you need to post an actual test case showing what you tried and the results you observed.

Not to mention all the appropriate version information for your compiler etc.

It is always possible that you are doing something wrong in your code (impossible to tell without seeing a full example). If we assume your code is correct, the next thing you should check is: Does the leak occur repeatedly, if you run your "demonstration" code in a loop? If that is not the case, i.e. only the first iteration causes a leak but subsequent iterations don't, then I would guess that the regex implementation allocates some "global" data structures at first use. These probably won't be free'd until the program terminates.

(If, instead, the memory leak adds up on every iteration, then you should be concerned)

Also, how exactly do you measure "memory used"? Keep in mind that C/C++ programs allocate dynamic memory from the "heap" space. And the "heap" space is a memory region managed by the C/C++ runtime. If the "heap" runs out of free space, then the C/C++ runtime will try to grow the "heap", by allocating more memory pages from the operating system – in which case you will see the "memory usage" of your process increase (e.g. in the Task Manager). But: The C/C++ runtime does not necessarily de-allocate "unused" heap memory pages as soon as possible, but instead may keep some "spare" memory pages for re-use (performance optimization). In other words, the "memory usage" you see in Task Manager or that you get via GetProcessMemoryInfo() may not be meaningful for detecting "small" memory leaks...
Last edited on
Topic archived. No new replies allowed.