Menu

memleak help

2021-06-24
2021-06-25
  • Amit Thukral

    Amit Thukral - 2021-06-24

    int function1 ()
    {
    char tmpStr;
    tmpStr = (char
    )malloc( 20sizeof(char) );
    int ret=sprintf(
    p, "\nthis is my string\n");
    function2(tmpStr);
    return ret;
    }

    As I understood, there is limitation that cppcheck cannot know if function2 would free the memory. Hence, it doesn't say anything whether function1 has memory leak or not.
    However, I see that there are couple of ways to get this reported by cppcheck by
    1) by using .cfg and using leak-ignore
    2) if developer knows, if there exists a function which frees it -- this is done by using "dealloc" and "use"

    My question is regarding- if I use "function2" in .cfg like below:
    <alloc>function1 </alloc>
    <dealloc>function2 </dealloc>
    i.e. telling cppcheck that function2 will is handling the deallocation. But, what if there is some bug in function2 or even if it doesn't free the memory at all, there is not error reported by cppcheck.

    I want to figure out if there is a way to tell cppcheck that resource will be freed by some function, but if that function doesn't free the memory in some scenarios, it should get reported by cppcheck. Is there any way ?

     
  • Daniel Marjamäki

    Your configuration <alloc>function1</alloc> <dealloc>function2</dealloc> looks strange.. function1 does not return allocated memory. If function2 deallocates the pointer it should use free because the memory passed to it was allocated by malloc..

    but if that function doesn't free the memory in some scenarios, it should get reported by cppcheck. Is there any way ?

    There is no way to configure such conditional deallocation currently.

     

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.