“2019年视觉演播室”有一个象样的记忆分析工具,可以在打字时通过互动使用,也可以通过节目制作(不打字),在以下两种情况下,我只举一个最低的例子。
主要想法是在这一过程的开始和结束时,对差距进行微调,然后对记忆状况进行比较,以发现潜在的记忆泄露。
Interactively
建立以下<代码>main.cpp文档(在新的专册中):
#include <string.h>
int main()
{
int a = 1;
char* s = new char[17];
strcpy_s(s,17,"stackoverflow_pb");
char* ss = new char[14];
strcpy_s(ss, 14,"stackoverflow");
delete[] ss;
return 0;
}
然后:
- Put a breakpoint on the first line "int a..."
- Click Debug > Windows > Show Diagnostic Tools; and pick memory usage
- Then debug the code (F5), when the breakpoint is hit, click
Take snapshot
on the Memory Usage summary toolbar.
- Go to the last line "return 0.." (
step over
(F10) several times) and take another snapshot.
- Click on the red arrow in the second snapshot (in memory usage tab)
- this will open a new "snapshot" tab that permits you to compare this snapshot with the first one (or another one) and to detect memory leaks. In this example there is a memory leak for variable
s
(stackoverflow_pb). You can find it by double click the "char[]" object.
上述程序的关键步骤见以下图象:
By programming
将守则改为:
#include <iostream>
#include "windows.h"
#define _CRTDBG_MAP_ALLOC //to get more details
#include <stdlib.h>
#include <crtdbg.h> //for malloc and free
int main()
{
_CrtMemState sOld;
_CrtMemState sNew;
_CrtMemState sDiff;
_CrtMemCheckpoint(&sOld); //take a snapshot
char* s = new char[17];
strcpy_s(s, 17, "stackoverflow_pb");
char* ss = new char[14];
strcpy_s(ss, 14, "stackoverflow");
delete[] ss;
_CrtMemCheckpoint(&sNew); //take a snapshot
if (_CrtMemDifference(&sDiff, &sOld, &sNew)) // if there is a difference
{
OutputDebugString(L"-----------_CrtMemDumpStatistics ---------");
_CrtMemDumpStatistics(&sDiff);
OutputDebugString(L"-----------_CrtMemDumpAllObjectsSince ---------");
_CrtMemDumpAllObjectsSince(&sOld);
OutputDebugString(L"-----------_CrtDumpMemoryLeaks ---------");
_CrtDumpMemoryLeaks();
}
return 0;
}
同样,但通过编码,你可以将其融入自动建筑系统,其职能是:_CrtMemCheckpoint
<<<<>>> 参考书目/参考书目/目录 相比之下,血清和回归的真实记忆有所不同。
Since it is the case, it enters the conditional block and prints details about the leaks via several functions (see _CrtMemDumpStatistics , _CrtMemDumpAllObjectsSince and _CrtDumpMemoryLeaks - the latter doesn t require snapshots).
参看该产出,在最后一行“返回0”上打了一个断点,打上了,并看着 de。 产出如下:
获取更多信息,见以下链接: