Valgrind reporting Mismatched free() / delete / delete []
Valgrind reporting Mismatched free() / delete / delete []
我正在编写一个将在多个系统上运行的库(其中一些系统没有malloc
或stdlib)。在我的stdlib(不同的lib)中,我重写了new
和delete
操作符以对函数进行泛型调用(本示例没有这些函数)。每个系统将覆盖这些泛型调用到各自的内存分配设备。
问题是当我试图这样做。下面是一些简化的示例代码来重现这个问题:
#include <cstdlib>
void* operator new(unsigned long size) {
return std::malloc(size); // would normally call an intermediate function which would be overridden by the system
}
void operator delete(void* object) {
std::free(object); // would normally call an intermediate function which would be overridden by the system
}
void operator delete(void* object, unsigned long size) {
std::free(object); // would normally call an intermediate function which would be overridden by the system
}
class MyClass {
};
int main() {
MyClass* myClass = new MyClass();
delete myClass;
}
当我使用普通gcc-6
(没有参数)构建它并使用valgrind
(没有参数)运行时,我得到这个错误:
==11219== Mismatched free() / delete / delete []
==11219== at 0x4C2DD6B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11219== by 0x108730: operator delete(void*, unsigned long) (in /home/chris13524/tmp/test.o)
==11219== by 0x10875A: main (in /home/chris13524/tmp/test.o)
==11219== Address 0x5200040 is 0 bytes inside a block of size 1 alloc'd
==11219== at 0x4C2D1AF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11219== by 0x108745: main (in /home/chris13524/tmp/test.o)
看起来delete
操作符工作正常,但是Valgrind覆盖了我覆盖的new
操作符。知道怎么解决这个问题吗?
删除中间函数不是一个选项,因为我有其他代码在那里。
它如何在我的实际程序中工作的示例(同样,没有在我的示例中显示):
new => create => <intermediate code> => createImpl => malloc
create => <intermediate code> => createImpl => malloc
我使用的是gcc v6.2.0, valgrind v3.12.0和Ubuntu 16.10
多亏了Paul Floyd,这个bug已经在提交6ef6f738a中修复了。查看bug报告
然而,此修复尚未发布(截至2018年6月),并且可能需要更长的时间才能在发行版中显示。如果您现在需要此修复,我建议从源代码构建。
相关文章:
- 运算符C++ "delete []"仅删除 2 个前值
- g++用户定义的动态链接库上的全局new和delete运算符
- 为什么"delete"关键字不删除节点?
- "delete"在 C++ 中实际上做了什么?
- 析构函数和'delete'之间的区别
- 如何知道何时调用删除以及何时调用 delete[] C++?
- 在对象指针上调用 Delete 是否会递归删除其动态分配的成员
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- 析构函数中的"delete this"
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- 即使在使用 delete[] 后仍保留的元素
- 如果我在下面的代码片段中添加"delete[] d;",为什么我得到零?
- C++原始指针和"delete"
- 为什么 std::vector 使用 std::分配器而不是运算符 new 和 delete?
- 重载 new 和 delete 会导致 valgrind 错误
- 为什么"delete"操作员给我访问权限冲突
- 析构函数是否会自动调用 delete[] C++?
- C++"::delete"的目的是什么?
- 使用 delete [] 运算符取消分配类中数据成员的内存
- Valgrind reporting Mismatched free() / delete / delete []