79329350

Date: 2025-01-04 18:13:37
Score: 0.5
Natty:
Report link

Here is a citation of what I found about [CWE-401] warning:

"First of all, CWE-401 is not an error, but rather a warning. The code will work even if it leaks memory, and under certain circumstances you may even want to leave some malloc without a free. CPPCheck is just warning you that you are doing something suspicious, and the rule of thumb is to free any memory that you allocate. If you don't have a precise reason to leave the memory allocated, try to free it.

There is no source for a memory leak per-se, in the sense that no specific line of code is triggering the error. The issue is that there is a missing free, but CPPCheck doesn't have a clear line to point to. My guess that would be that the two lines highlighted are the line where the lifetime of the reference starts (i.e. the initial malloc) and where it seems to end (i.e. the last use of the reference). CPPCheck noticed that there is no free where the lifetime ends, which is probably why it suspects a memory leak.

A simplified view of the memory is that it is split in 2 parts when used in C: heap and stack. The stack contains all the small values and is freed automatically upon leaving the current function. The heap is a user-managed section of the memory where values can be allocated (with malloc), and released (with free). So no, memory allocated with malloc is not freed when leaving the function."

- roadelou

Link to the reddit post

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Low reputation (1):
Posted by: Miguel Andrés Rendón Reyes