0

I have encountered "still reachable" and "possibly lost" leaks in valgrind from a lot of library code (OSX), but I haven't seen anything within a program that I have written that has that. For example, the following produces a "definitely lost" leak:

#include <stdlib.h>
int main(void)
{
    malloc(10)
    return 0;
}

What would be a code example of a still-reachable leak or a possibly-lost leak?

David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    There's [this question](https://stackoverflow.com/questions/3840582/still-reachable-leak-detected-by-valgrind) where OP encounters a `still-reachable` leak, the accepted answer explains what exactly it is pretty well. – mediocrevegetable1 Apr 13 '21 at 03:51
  • 1
    Try assigning the result of `malloc(10)` to a global variable. – Nate Eldredge Apr 13 '21 at 04:01
  • The linked answer is poor advice. You need to look at your 'still reachables' carefullly. If they are one-off allocations then it is fairly safe to ignore them. If they are allocations that keep growing then there may be a problem. There may be a performance hit if you do not free memory that is no longer needed. But there may also be a performance hit if you do lots of unnecessary freeing. In this case there is no simple answer. – Paul Floyd Apr 14 '21 at 07:43
  • 1
    And see my answer here for 'possibly lost' leaks https://stackoverflow.com/questions/45912758/valgrind-possibly-lost-memory – Paul Floyd Apr 14 '21 at 07:46

0 Answers0