Inspired by this post in a recent discussion also involving leaks and loops I tested if a leak is detected when the pointer returned from malloc is stored in a member of a struct.
Indeed, in the following code cppcheck detects a memory leak:
The first case should be handled by checkScope(), which however bails out when it encounters a loop. The second leak is found by checkStructVariable().
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Using the snippet below cppcheck reports 'memleak' in addition to 'unreadVariable' and 'unusedAllocatedMemory':
cppcheck testmemleak.c --enable=all --inconclusive
But 'memleak' is no longer reported when the code includes a for loop:
'unreadVariable' and 'unusedAllocatedMemory' are still reported however.
Is this a bug or am I missing something obvious?
Same behavior using a dummy while loop. Tested cppcheck versions 2.6, 1.9 and HEAD.
Inspired by this post in a recent discussion also involving leaks and loops I tested if a leak is detected when the pointer returned from malloc is stored in a member of a struct.
Indeed, in the following code cppcheck detects a memory leak:
The first case should be handled by
checkScope()
, which however bails out when it encounters a loop. The second leak is found bycheckStructVariable()
.