内存泄漏跟踪
memory leak trace
>我已经覆盖了我的运算符 new[] 作为
void* operator new[](std::size_t sz, const char *file, int line)
{
void* mem = malloc(sz);
if(mem == 0){
printf("Could not allocate the desired memory, the new operator failsn");
std::abort();
}
printf("Allocation has been done!n");
printf("Allocation has been done! In %s, line #%i, %p[%i]n", file, line, mem, sz);
return mem;
}
#define DEBUG_NEW2 new[](__FILE__, __LINE__)
#define new[] DEBUG_NEW2
我的程序主要使用这种类型的新运算符,这就是为什么我更关心它。但是,编译器给我"宏名称 [-Werror] 后缺少空格"错误消息。我试图玩弄"#define 新的[]DEBUG_NEW2"。在某些情况下,它可以编译正常,但是我不会被覆盖new[]。
这里的问题是以#define new[]
开头的行
您正在尝试创建名为 new[]
的宏。
首先,这是非法的,因为宏名称必须是有效的标识符,这意味着它只包含字母、下划线、数字,并且不能以数字开头。
其次,您正在尝试为关键字赋予新的含义。这是不允许的,这意味着您的程序无效或提供未定义的行为。
无需添加这样的宏即可重载运算符 new[]。实际上,您需要做的就是在所需范围内声明一个带有签名void* operator new[](size_t)
的函数,它将自动用于数组分配。
相关文章:
- valgrind-hellgrind与泄漏检查的结果不同
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 光线跟踪器灯光反射错误
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 人脸跟踪arduino代码的优化
- 跟踪滚动条上的鼠标事件
- 尽管遵循了规则,内存泄漏在哪里
- 为什么调用堆栈数组会导致内存泄漏
- 如何使用新运算符跟踪在循环中创建的 QLabel
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用模板类的自定义列表类型中的内存泄漏
- Valgrind内存泄漏错误的文件跟踪
- 无法跟踪 Visual Studio 内存泄漏
- 跟踪插件中的内存泄漏,使用_crtBreakAlloc, _CRTDBG_MAP_ALLOC
- 无法跟踪Linux中的内存泄漏
- 跟踪内存泄漏
- 内存泄漏跟踪
- 跟踪GPU内存泄漏的专业方法(分配而不释放)
- 跟踪WinSock MFC应用程序句柄泄漏的来源