tft每日頭條

 > 科技

 > c++ 如何避免内存洩漏

c++ 如何避免内存洩漏

科技 更新时间:2024-07-29 18:09:08
前言

寫c 的程序員都應該對申請内存和釋放内存有着深刻的領悟(可能有些初級用着前人封裝的智能指針感受不深)。同時對于出現崩潰生成可以調試的dump文件也極為重要,對于win下的發布版程序很重要。

工具

crtdbg偵測内存洩露,dbghelp 生産MINIDUMP

内存洩漏是在vs開發中,程序結束在vs輸出裡面會提示有沒有内存洩露。dump文件是在程序運行工程中崩潰時候捕捉異常後生成的,要配合對應的pdb文件和代碼用vs進行分析。

實例

樣例中的 memorytools.cpp memorytools.h 可以直接拿過去用的,不需要額外引用頭文件和庫。

代碼目錄:

├── main.cpp├── memorytools.cpp (附在文章後面面)└──memorytools.h(附在文章後面面)

1.内存洩漏檢測

main.cpp

#include "memorytools.h" int main() { //設置生成dump文件名 lge::CMemoryTools::initAppName("test"); //初始化 lge::CMemoryTools::initDebug(); //測試内存洩露 char * p = (char*) malloc(1000); int* pInt = new int; return 0; }

運行後提示如下:

c++ 如何避免内存洩漏(内存洩漏檢測和dump文件生成)1

如果申請的内存,在程序退出後還沒有釋放,會提示你内存洩漏。當然很多項目用的單例,在程序關閉的時候,不釋放一樣會提示内存洩漏。

如果想知道内存是哪一步申請的,比如我圓圈圈出的63,可以直接在CMemoryTools::initDebug函數内将_CrtSetBreakAlloc(0); 改成 _CrtSetBreakAlloc(63); 這種vs在調試的時候,就會自動斷點到這步的内存申請(但對于大項目這種方式其實不适用)。

2.dump生成及調試

#include "Memorytools.h" class CTest { public: int p; }; int main() { //設置生成dump文件名 lge::CMemoryTools::initAppName("test"); //初始化 lge::CMemoryTools::initDebug(); //測試崩潰 CTest* pTest = NULL; pTest->p = 1; return 0; }

在exe所在目錄直接雙擊打開,會看到生成的dump文件。

c++ 如何避免内存洩漏(内存洩漏檢測和dump文件生成)2

保障exe pdb 和代碼都是對應的關系,将dmp文件直接拖進vs項目即可。

c++ 如何避免内存洩漏(内存洩漏檢測和dump文件生成)3

之後就可以直接調試了。

memorytools.h

#pragma once #if WIN32 #include <Windows.h> #include <DbgHelp.h> #pragma warning(disable:4091) #pragma comment(lib,"Dbghelp.lib") #include <tchar.h> #include <stdlib.h> #include <stdio.h> #include <atlstr.h> #include <string> #ifdef DEBUG # ifdef _MSC_VER # ifndef _CrtDBG_MAP_ALLOC # define _CRTDBG_MAP_ALLOC # endif #ifndef _MAPNEW #define _MAPNEW #endif # ifdef _MAPNEW # ifndef _CRTDBG_MAP_ALLOC_NEW # define _CRTDBG_MAP_ALLOC_NEW # endif # endif # include <crtdbg.h> # ifdef _MAPNEW # ifndef new # define DEBUG_NORMALBLOCK new(_NORMAL_BLOCK, __FILE__, __LINE__) # define new DEBUG_NORMALBLOCK # endif # endif # endif #endif #ifndef __FILE_LINE__ # define _TLN(LN) #LN # define __TLINE__(LN) _TLN(LN) # define __FILE_LINE__ __FILE__"("__TLINE__(__LINE__)")" #endif #define APP_NAME_TYPE CString #else #define APP_NAME_TYPE std::string #endif namespace lge { /* * windows下内存洩漏檢測工具 */ class CMemoryTools { public: static void initAppName(const APP_NAME_TYPE& name); static void initDebug(); static APP_NAME_TYPE __app_name__; static bool _initDebug; }; }

memorytools.cpp

#include "memorytools.h" APP_NAME_TYPE lge::CMemoryTools::__app_name__ = "engine"; bool lge::CMemoryTools::_initDebug = false; #if WIN32 int MemoryLeakReportRoutine(int blockType, char *message, int *p) { if (strcmp(message, "Object dump complete.\n") == 0) { static const char * szSolvMemLeak = "請解決内存洩露!\r\n"; OutputDebugStringA(szSolvMemLeak); } return 0; } LONG WINAPI UnHANDLEExceptionFilte(EXCEPTION_POINTERS *ExceptionInfo) { SYSTEMTIME Systime; GetLocalTime(&Systime); CString Str; Str.Format(_T("%s_%d-%d-%d-d-d-d.dmp"), lge::CMemoryTools::__app_name__, Systime.wYear, Systime.wMonth, Systime.wDay, Systime.wHour, Systime.wMinute, Systime.wSecond); HANDLE hFile = CreateFile(Str, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); MiniDump_TYPE wDumpFlag = MiniDumpWithFullMemory; if (hFile != INVALID_HANDLE_VALUE) { MINIDUMP_EXCEPTION_INFORMATION ExInfo; ExInfo.ThreadId = GetCurrentThreadId(); ExInfo.ExceptionPointers = ExceptionInfo; ExInfo.ClientPointers = NULL; MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, wDumpFlag, &ExInfo, NULL, NULL); CloseHandle(hFile); } ExitProcess(-1); return 0; } #endif //WIN32 void lge::CMemoryTools::initAppName(const APP_NAME_TYPE& name) { __app_name__ = name; } void lge::CMemoryTools::initDebug() { if (_initDebug) { return; } _initDebug = true; #ifdef WIN32 #ifdef DEBUG _set_error_mode(_OUT_TO_MSGBOX); _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG); _CrtSetReportHook(&MemoryLeakReportRoutine); _CrtSetBreakAlloc(0); #endif // DEBUG SetUnhandledExceptionFilter(UnhandleExceptionFilte); #endif }

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved