Valgrind "Mismatched new/delete size" not reported as a memory issue

Hello,

I recently encounter a Mismatched new/delete size value reported by valgrind but captured by cmake and thus not reported on our dashboard.

The problem seems to be a missing regex in ProcessMemCheckValgrindOutput which only reports R"(== .*Mismatched free\(\) / delete / delete \[\])") as Mismatched deallocation.

Would it be possible to add a specific regex or extend the existing one?

1 Like

I’d welcome a merge request to do that, yes, thanks.

1 Like

I made an issue here: https://gitlab.kitware.com/cmake/cmake/-/work_items/28149

The merge request is in progress… @npoletz , may you write a simplest minimal reproducer for this, so I could make a proper unit test please?

Hi,
Thanks for following up. I haven’t had time to fix the issue myself, but I can provide a reproducer. Would you prefer that I post it here or in the issue ?

A simple reproducer with a missing virtual dtor is provided below :

#include <memory>
#include <vector>
#include <iostream>

// Base class with NON-virtual destructor
class Base {
public:
    ~Base() { std::cout << "Base destructor called\n"; }
};

class Derived : public Base {
    int data = 0;
public:
    Derived() : Base()
    {
        std::cout << "Derived constructor\n";
    }

    ~Derived() {
        std::cout << "Derived destructor called\n";
    }
};

int main() {
    
    std::unique_ptr<Base> ptr = std::make_unique< Derived>();
    return 0;
}

And gives the following valgrind report:

==159787== Mismatched new/delete size value: 1
==159787==    at 0x484A5B9: operator delete(void*, unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==159787==    by 0x109698: std::default_delete<Base>::operator()(Base*) const (unique_ptr.h:99)
==159787==    by 0x1094B7: std::unique_ptr<Base, std::default_delete<Base> >::~unique_ptr() (unique_ptr.h:404)
==159787==    by 0x109220: main (mismatched_free_delete_reproducer.cpp:27)
==159787==  Address 0x4e42080 is 0 bytes inside a block of size 4 alloc'd
==159787==    at 0x4846FA3: operator new(unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==159787==    by 0x109318: std::__detail::_MakeUniq<Derived>::__single_object std::make_unique<Derived>() (unique_ptr.h:1070)
==159787==    by 0x1091F0: main (mismatched_free_delete_reproducer.cpp:25)
==159787== 
==159787== 
==159787== HEAP SUMMARY:
==159787==     in use at exit: 0 bytes in 0 blocks
==159787==   total heap usage: 3 allocs, 3 frees, 74,756 bytes allocated
==159787== 
==159787== All heap blocks were freed -- no leaks are possible
==159787== 
==159787== For lists of detected and suppressed errors, rerun with: -s
==159787== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)


1 Like

Thanks, will adopt it as a test case here: https://gitlab.kitware.com/cmake/cmake/-/work_items/28149 :+1: