intmain(){ int* var = newint[10]; _CrtDumpMemoryLeaks(); return0; }
注意要以调试模式运行该程序,如果不以调试模式运行则不会显示内存泄漏报告。
可以看到以下输出
1 2 3 4 5
Detected memory leaks! Dumping objects -> {105} normal block at 0x00D68830, 40 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete.
intmain(){ int* var = newint[10]; _CrtDumpMemoryLeaks(); return0; }
再次以调试模式运行该程序,发现正确输出了文件名和行号信息
1 2 3 4 5
Detected memory leaks! Dumping objects -> C:\path\main.cpp(13) : {105} normal block at 0x009F8830, 40 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete.
intmain() { using RowMajorMat = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>; RowMajorMat a(1024, 4096); RowMajorMat b(4096, 784); constauto c = a * b; std::cout << c(0, 0) << std::endl; _CrtDumpMemoryLeaks(); }
发现内存泄漏
1 2 3 4 5 6 7
Detected memory leaks! Dumping objects -> C:\path\eigen3\Eigen\src\Core\util\Memory.h(88) : {176} normal block at 0x02446040, 12845072 bytes long. Data: < @`D > CD CD CD CD CD CD CD CD CD CD CD CD 40604402 C:\path\eigen3\Eigen\src\Core\util\Memory.h(88) : {175} normal block at 0x01338040, 16777232 bytes long. Data: < @ 3 > CD CD CD CD CD CD CD CD CD CD CD CD 40803301 Object dump complete.
但是实际上并没有发生内存泄漏,这是由于变量 a, b, c 在执行 _CrtDumpMemoryLeaks 还没有被销毁。解决方法是加上一个大括号。
1 2 3 4 5 6 7 8 9 10 11
int main() { { using RowMajorMat = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>; RowMajorMat a(1024, 4096); RowMajorMat b(4096, 784); const auto c = a * b; std::cout << c(0, 0) << std::endl; } _CrtDumpMemoryLeaks(); }