我似乎无法将MS泄漏检测器用于新表达式“new(std::nothrow)”.是这样吗

It seems like I cannot use MS leak detector for the new expression `new (std::nothrow)`. Is that correct?

本文关键字:std new nothrow 是这样吗 表达式 MS 用于 检测器 泄漏      更新时间:2023-10-16

我正在尝试使用新表达式new (std::nothrow)获取发生泄漏的文件和行。

下面代码中有注释的新表达式未编译。

#include <iostream>
int main()
{
    #ifdef _DEBUG
    int* p = new (_NORMAL_BLOCK, __FILE__, __LINE__) int(10);
//  int* q = new (std::nothrow, _NORMAL_BLOCK, __FILE__, __LINE__) int(10); 
    #else
    int* p = new int(10);
    int* q = new int(10);
    #endif
    _CrtDumpMemoryLeaks();
}

我也很想知道<iostream>在哪里包括<crtdbg.h>。我就是找不到它。但当然,它一定在那里,在某个地方。

不直接。微软不提供void* operator new(std::nothrow_t, const char* file, int line),但你可以自己提供。只需转发到抛出版本和catch处理程序return NULL;中。