Menu

Memory leak not detected

Rutger
2019-08-14
2019-08-14
  • Rutger

    Rutger - 2019-08-14

    If I have this code:

    void func()
    {}
    
    void leak()
    {
        int *p = new int;
        *p = 42;
    }
    
    int main()
    {
        leak();
        return 0;
    }
    

    Cppcheck correctly detects a memory leak:

    $ cppcheck --enable=style --template=gcc leak.cc
    Checking leak.cc ...
    leak.cc:8:1: warning: Memory leak: p [memleak]
    }
    ^
    

    However if I add some code to leak() Cppcheck no longer detects the leak. Even if this code does not reference variable p:

    void leak()
    {
        int *p = new int;
        *p = 42;
        func();
    }
    
    $ cppcheck --enable=style leak.cc
    Checking leak.cc ...
    

    Is this a limitation of Cppcheck or is this a bug?

    This is with Cppcheck 1.86 on CentOS 7.

     
  • Daniel Marjamäki

    I am not sure what happens.

    But it is not conclusive if there is a leak or not. func() may exit. A function call at the end of a scope can often cause bailouts. Put some code after the function call and cppcheck will not assume that it exit. Or do something with the return value of func().

    But if the check assumes that func() exits then the --check-library should have warned about this.. but I do not see a warning.

     

    Last edit: Daniel Marjamäki 2019-08-14
  • Daniel Marjamäki

    I created this trac ticket: https://trac.cppcheck.net/ticket/9279

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.