C/C++???й??????
???????????? ???????[ 2015/4/1 14:39:18 ] ????????C++ ??? й? ???
????STEP2?? ??????????????????????????????а????????????????÷?????????λ??????????????й??????
????_CrtDumpMemoryLeaks();
?????????????????????£?
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
using namespace std;
void GetMemory(char *p?? int num)
{
p = (char*)malloc(sizeof(char) * num);
}
int main(int argc??char** argv)
{
char *str = NULL;
GetMemory(str?? 100);
cout<<"Memory leak test!"<<endl;
_CrtDumpMemoryLeaks();
return 0;
}
??????????????????г??????_CrtDumpMemoryLeaks ????“???”????????????й?????? ???й?????????????
????????????? #define _CRTDBG_MAP_ALLOC ??????й???????????????
????δ???? _CRTDBG_MAP_ALLOC ??????????????
?????????????????????????
?????????????????????? CRT????
????“?????”????????????????檔
????“??????”???? MFC ????????????????????????????????????顣 MFC new ??????????????????????????????????????顣
????“CRT ??”???? CRT ??????????????????顣 CRT ??????Щ????????????????????????й??????п?????Щ?飬?????????????????? CRT ????????
??????????????й?????п?????????????????
????“?????”???????????顣
????“?????”????????????飬??????????????й??????С?
???????????????????λ?á?
????????????λ????С??
????? 16 ??????????????????????
?????????? _CRTDBG_MAP_ALLOC ???????????????з???й???????????? ????????????е?????????????? 10?????????е??к??
??????????????????????λ??????????? _CrtDumpMemoryLeaks ?????????? ??????????λ???????????????????????????λ?÷???? _CrtDumpMemoryLeaks ???????????????????????????μ????
????_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
????????????????????????? _CrtDumpMemoryLeaks?? ?????????? _CRTDBG_ALLOC_MEM_DF ?? _CRTDBG_LEAK_CHECK_DF ????λ????????????
????2.2????λ????????й????
?????????????????????????????λ???????????????????亯??malloc??new??????????е?GetMemory?????У?????10?У?????????λ????????????????GetMemory()???μ????й????????????????п????к??????GetMemory????????λ??????????????GetMemory???μ????й???
??????λ???й????????????漰?????????ó??????????????? CRT ???????????? _CrtMemState???????????洢???????????
????_CrtMemState s1?? s2?? s3;
?????????????????????????????? _CrtMemCheckpoint ???????? _CrtMemState ???? ?ú????????????????????????
????_CrtMemCheckpoint( &s1 );
????????? _CrtMemDumpStatistics ???????? _CrtMemState ???????????????????y????????
????_CrtMemDumpStatistics( &s1 );
????????????????????????????????й?????????ò???????????????????????????? _CrtMemDifference ?????????????
????_CrtMemCheckpoint( &s1 );
????// memory allocations take place here
????_CrtMemCheckpoint( &s2 );
????if ( _CrtMemDifference( &s3?? &s1?? &s2) )
????_CrtMemDumpStatistics( &s3 );
??????

???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11