Is there any way to check if array was fully freed and nothing was left behind? Valgrind only says that there are some unfreed blocks. Is debugger or something like that made just for this?
有没有办法检查数组是否完全释放,没有留下任何东西? Valgrind只说有一些不一致的块。是调试器还是类似的东西?
1 个解决方案
#1
2
So, no, there is no way to know if you have freed everything just by looking at the pointers. You must know your data structure and algorithm and know when to release alocated memory.
所以,不,没有办法知道你是否通过查看指针来释放所有内容。您必须知道您的数据结构和算法,并知道何时释放已分配的内存。
Under Windows (MSC) I use the following to check at the end, when the program terminates, if I have freed all allocated memory:
在Windows(MSC)下,我使用以下内容检查程序何时终止,如果我已释放所有已分配的内存:
// Check heap upon exit: all memory freed and not corrupted?
#include <crtdbg.h>
_CrtMemState memStateStart, memStateEnd, memStateDelta;
int WinMain(...
{
...
// Make a checkpoint of the heap's state so we can later check the heap is still OK
_CrtMemCheckpoint( &memStateStart );
ghMainWnd = CreateWindow( // Create the app. main window
...
);
...
}
...
WndProc(hWnd, msg,...)
{
...
case WM_CLOSE:
// Check the heap
_CrtMemCheckpoint( &memStateEnd );
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_WNDW );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_WNDW );
_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_WNDW );
if (_CrtMemDifference( &memStateDelta, &memStateStart, &memStateEnd ))
_CrtMemDumpStatistics( &memStateDelta );
_CrtDumpMemoryLeaks();
DestroyWindow (hWnd);
return (0);
#1
2
So, no, there is no way to know if you have freed everything just by looking at the pointers. You must know your data structure and algorithm and know when to release alocated memory.
所以,不,没有办法知道你是否通过查看指针来释放所有内容。您必须知道您的数据结构和算法,并知道何时释放已分配的内存。
Under Windows (MSC) I use the following to check at the end, when the program terminates, if I have freed all allocated memory:
在Windows(MSC)下,我使用以下内容检查程序何时终止,如果我已释放所有已分配的内存:
// Check heap upon exit: all memory freed and not corrupted?
#include <crtdbg.h>
_CrtMemState memStateStart, memStateEnd, memStateDelta;
int WinMain(...
{
...
// Make a checkpoint of the heap's state so we can later check the heap is still OK
_CrtMemCheckpoint( &memStateStart );
ghMainWnd = CreateWindow( // Create the app. main window
...
);
...
}
...
WndProc(hWnd, msg,...)
{
...
case WM_CLOSE:
// Check the heap
_CrtMemCheckpoint( &memStateEnd );
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_WNDW );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_WNDW );
_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_WNDW );
if (_CrtMemDifference( &memStateDelta, &memStateStart, &memStateEnd ))
_CrtMemDumpStatistics( &memStateDelta );
_CrtDumpMemoryLeaks();
DestroyWindow (hWnd);
return (0);