| Summary: | bogus "Mismatched new/delete size" for delete of pointer to base class | ||
|---|---|---|---|
| Product: | [Developer tools] valgrind | Reporter: | Olly Betts <ojwbetts+kde> |
| Component: | memcheck | Assignee: | Julian Seward <jseward> |
| Status: | RESOLVED NOT A BUG | ||
| Severity: | normal | CC: | pjfloyd, tom |
| Priority: | NOR | ||
| Version First Reported In: | 3.25.1 | ||
| Target Milestone: | --- | ||
| Platform: | Debian unstable | ||
| OS: | Linux | ||
| Latest Commit: | Version Fixed/Implemented In: | ||
| Sentry Crash Report: | |||
|
Description
Olly Betts
2025-11-10 21:07:00 UTC
The first problem with this bug report is that you are using pejorative language to describe the issue. Just stick to the facts. The real problem is that your code is wrong. You did not read this part of the cppreference description: "If ptr is a pointer to a base class subobject of the object that was allocated with new, the destructor of the base class must be virtual, otherwise the behavior is undefined. " Is your destructor virtual? No. So your code has UB and Valgrind is correctly diagnosing a genuine error. If I add a destuctor the B and rerun your code I get no errors. Specifically what is happening is that because the destructor is not virtual you are calling the base class destructor, which tries to free a base class object, which is only 4 bytes in size. If the destructor was virtual it would have called the derived class destructor, which would free a derived class object, which would be 8 bytes in size. Well actually it would probably be 16 bytes then because adding the virtual destructor would have added a vtable pointer... Aha, thanks - I had indeed missed that later note about needing a virtual destructor. Sorry about the noise. (I'm still puzzled why this seems to have only started to trigger recently though...) This is a relatively new feature, it was added in March 2023 (see below) and released with Valgrind 3.22 in October of the same year. This bug report mentions Debian unstable. If I understand this changelog correctly https://metadata.ftp-master.debian.org/changelogs//main/v/valgrind/valgrind_3.25.1-3_changelog then the Debian port switched from 3.20 (Oct 2022) to 3.24 (Oct 2024) in January this year. Author: Paul Floyd <pjfloyd@wanadoo.fr> 2023-03-12 08:26:04 Committer: Paul Floyd <pjfloyd@wanadoo.fr> 2023-09-02 16:12:35 Parent: 6489bc63a13fcf614cdb1cb318ea9a1f898a39cd (regtest: make memcheck sem test quiet) Child: 86a8f04c7f5d31050d841e82397f052fc6a958a6 (regtest: silence a few warnings) Branches: master, remotes/origin/master and many more (23) Follows: VALGRIND_3_21_0 Precedes: VALGRIND_3_22_0 Add memcheck errors for aligned and sized allocations and deallocations Bug 433857 Add validation to C++17 aligned new/delete alignment size Bug 433859 Add mismatched detection to C++ 17 aligned new/delete Bug 466105 aligned_alloc problems, part 2 Bug 467441 Add mismatched detection to C++ 14 sized delete |