主頁 > 軟體工程 > 記憶體泄漏!!!

記憶體泄漏!!!

2020-09-22 06:30:45 軟體工程

最近寫的MFC專案快結束了,正在進行整存泄露檢測。發現一處很特別的地方發生了記憶體泄漏,泄露提示如下:
Detected memory leaks!
Dumping objects ->
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp(4500) : {563} client block at 0x0184A3C8, subtype c0, 56 bytes long.
a CObject object at $0184A3C8, 56 bytes long
{548} normal block at 0x01849438, 40 bytes long.
 Data: <8   8   8       > 38 94 84 01 38 94 84 01 38 94 84 01 CD CD CD CD 
{547} normal block at 0x018492B8, 8 bytes long.
 Data: <  I     > D4 FD 49 00 00 00 00 00 
d:\vs2010\ect1\ect1\ect1.cpp(59) : {544} client block at 0x01849210, subtype c0, 12 bytes long.
a CObject object at $01849210, 12 bytes long
{543} client block at 0x01849330, subtype c0, 204 bytes long.
a CObject object at $01849330, 204 bytes long
使用_CrtSetBreakAlloc(563); 定位,使用shitf+F11,想上回溯到了一句系統自動生成的陳述句:
CMenu* pSysMenu = GetSystemMenu(FALSE);
說先申明:本程式并沒有對主頁面添加選單。

還有一種記憶體泄漏比較嚴重的是這樣提示的:、
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {443} normal block at 0x01846478, 22 bytes long.
 Data: <T vx            > 54 CD 76 78 05 00 00 00 05 00 00 00 01 00 00 00 
這種提示多大好幾十處,我覺得是CString變數使用不規范造成的,但是一時也想不到原因。

大家幫忙分析下,我這兩種記憶體泄漏到底出錯在哪里了。

uj5u.com熱心網友回復:

查哪里使用了  new

uj5u.com熱心網友回復:

檢查 new 出來的東西,一定要 delete. 

d:\vs2010\ect1\ect1\ect1.cpp(59)  是什么? 

uj5u.com熱心網友回復:

請看這篇文章http://www.cppblog.com/oyrp/articles/102251.html

或者注釋一部分代碼進行檢測

uj5u.com熱心網友回復:

CString::GetBuffer 
LPTSTR GetBuffer( int nMinBufLength );
throw( CMemoryException );

Return Value

An LPTSTR pointer to the object’s (null-terminated) character buffer.

Parameters

nMinBufLength

The minimum size of the character buffer in characters. This value does not include space for a null terminator.

Remarks

Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.

If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions. 

The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.

The buffer memory will be freed automatically when the CString object is destroyed. 

Note that if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer and ReleaseBuffer will perform a strlen on the buffer to determine its length. 

Example

The following example demonstrates the use of CString::GetBuffer.

// example for CString::GetBuffer
CString s( "abcd" );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
LPTSTR p = s.GetBuffer( 10 );
strcpy( p, "Hello" );   // directly access CString buffer
s.ReleaseBuffer( );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif

CString Overview |  Class Members |  Hierarchy Chart

See Also   CString::GetBufferSetLength, CString::ReleaseBuffer

uj5u.com熱心網友回復:

GetSystemMenu
The GetSystemMenu function allows the application to access the window menu (also known as the system menu or the control menu) for copying and modifying. 

HMENU GetSystemMenu(
  HWND hWnd,    // handle to window to own window menu
  BOOL bRevert  // reset flag
);
 
Parameters
hWnd 
Handle to the window that will own a copy of the window menu. 
bRevert 
Specifies the action to be taken. If this parameter is FALSE, GetSystemMenu returns the handle to the copy of the window menu currently in use. The copy is initially identical to the window menu, but it can be modified. 
If this parameter is TRUE, GetSystemMenu resets the window menu back to the default state. The previous window menu, if any, is destroyed. 

Return Values
If the bRevert parameter is FALSE, the return value is the handle to a copy of the window menu. If the bRevert parameter is TRUE, the return value is NULL. 

Remarks
Any window that does not use the GetSystemMenu function to make its own copy of the window menu receives the standard window menu. 

The window menu initially contains items with various identifier values, such as SC_CLOSE, SC_MOVE, and SC_SIZE. 

Menu items on the window menu send WM_SYSCOMMAND messages. 

All predefined window menu items have identifier numbers greater than 0xF000. If an application adds commands to the window menu, it should use identifier numbers less than 0xF000. 

The system automatically grays items on the standard window menu, depending on the situation. The application can perform its own checking or graying by responding to the WM_INITMENU message that is sent before any menu is displayed. 

Windows CE: Windows CE does not support a system menu, but GetSystemMenu is implemented as a macro to maintain compatibility with existing code. You can use the menu handle returned by this macro to disable the close box the same way you would in a Windows desktop platform. There is no other use for the return value in Windows CE. The brevert parameter is ignored. Use the following code to disable the Close button:

EnableMenuItem (GetSystemMenu(hwnd, FALSE), SC_CLOSE,MF_BYCOMMAND | MF_GRAYED);
QuickInfo
  Windows NT: Requires version 3.1 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Requires version 1.0 or later.
  Header: Declared in winuser.h.
  Import Library: Use user32.lib.

See Also
Menus Overview, Menu Functions, GetMenu, WM_INITMENU, WM_SYSCOMMAND 

 

uj5u.com熱心網友回復:

參考 1 樓 schlafenhamster 的回復:
查哪里使用了  new

new的都delete,都查過了。

uj5u.com熱心網友回復:

參考 2 樓 easunlee 的回復:
檢查 new 出來的東西,一定要 delete. 

d:\vs2010\ect1\ect1\ect1.cpp(59)  是什么? 

CShellManager *pShellManager = new CShellManager;
系統自帶的。

uj5u.com熱心網友回復:

參考 4 樓 zhao4zhong1 的回復:
CString::GetBuffer 
LPTSTR GetBuffer( int nMinBufLength );
throw( CMemoryException );

Return Value

An LPTSTR pointer to the object’s (null-terminated) character buffer.

Parameters

nMinBufLength

The minimum size of the character buffer in characters. This value does not include space for a null terminator.

Remarks

Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.

If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions. 

The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.

The buffer memory will be freed automatically when the CString object is destroyed. 

Note that if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer and ReleaseBuffer will perform a strlen on the buffer to determine its length. 

Example

The following example demonstrates the use of CString::GetBuffer.

// example for CString::GetBuffer
CString s( "abcd" );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
LPTSTR p = s.GetBuffer( 10 );
strcpy( p, "Hello" );   // directly access CString buffer
s.ReleaseBuffer( );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif

CString Overview |  Class Members |  Hierarchy Chart

See Also   CString::GetBufferSetLength, CString::ReleaseBuffer

我并沒有使用GetBuffer這個函式。

uj5u.com熱心網友回復:

在 DestroyWindow 中 
if (pShellManager != NULL)
{
delete pShellManager;
}

uj5u.com熱心網友回復:

參考 9 樓 schlafenhamster 的回復:
在 DestroyWindow 中 
if (pShellManager != NULL)
{
delete pShellManager;
}

不對,這段代碼在class CECTApp : public CWinApp里面。CWinApp并不存在DestroyWindow 這個函式,而且有問題的代碼是系統自動生成的,難道系統自己生成的代碼也有問題;代碼如下:
BOOL CECT1App::InitInstance()
{
// 如果一個運行在 Windows XP 上的應用程式清單指定要
// 使用 ComCtl32.dll 版本 6 或更高版本來啟用可視化方式,
//則需要 InitCommonControlsEx()。否則,將無法創建視窗。
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// 將它設定為包括所有要在應用程式中使用的
// 公共控制元件類。
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);

CWinApp::InitInstance();


AfxEnableControlContainer();

// 創建 shell 管理器,以防對話框包含
// 任何 shell 樹視圖控制元件或 shell 串列視圖控制元件。
CShellManager *pShellManager = new CShellManager;


// 標準初始化
// 如果未使用這些功能并希望減小
// 最終可執行檔案的大小,則應移除下列
// 不需要的特定初始化例程
// 更改用于存盤設定的注冊表項
// TODO: 應適當修改該字串,
// 例如修改為公司或組織名
SetRegistryKey(_T("應用程式向導生成的本地應用程式"));

CECT1Dlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: 在此放置處理何時用
//  “確定”來關閉對話框的代碼
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置處理何時用
//  “取消”來關閉對話框的代碼
}
// 洗掉上面創建的 shell 管理器。
if (pShellManager != NULL)
{
delete pShellManager;
}


// 由于對話框已關閉,所以將回傳 FALSE 以便退出應用程式,
//  而不是啟動應用程式的訊息泵。
return FALSE;
}

uj5u.com熱心網友回復:

參考 5 樓 zhao4zhong1 的回復:
GetSystemMenu
The GetSystemMenu function allows the application to access the window menu (also known as the system menu or the control menu) for copying and modifying. 

HMENU GetSystemMenu(
  HWND hWnd,    // handle to window to own window menu
  BOOL bRevert  // reset flag
);
 
Parameters
hWnd 
Handle to the window that will own a copy of the window menu. 
bRevert 
Specifies the action to be taken. If this parameter is FALSE, GetSystemMenu returns the handle to the copy of the window menu currently in use. The copy is initially identical to the window menu, but it can be modified. 
If this parameter is TRUE, GetSystemMenu resets the window menu back to the default state. The previous window menu, if any, is destroyed. 

Return Values
If the bRevert parameter is FALSE, the return value is the handle to a copy of the window menu. If the bRevert parameter is TRUE, the return value is NULL. 

Remarks
Any window that does not use the GetSystemMenu function to make its own copy of the window menu receives the standard window menu. 

The window menu initially contains items with various identifier values, such as SC_CLOSE, SC_MOVE, and SC_SIZE. 

Menu items on the window menu send WM_SYSCOMMAND messages. 

All predefined window menu items have identifier numbers greater than 0xF000. If an application adds commands to the window menu, it should use identifier numbers less than 0xF000. 

The system automatically grays items on the standard window menu, depending on the situation. The application can perform its own checking or graying by responding to the WM_INITMENU message that is sent before any menu is displayed. 

Windows CE: Windows CE does not support a system menu, but GetSystemMenu is implemented as a macro to maintain compatibility with existing code. You can use the menu handle returned by this macro to disable the close box the same way you would in a Windows desktop platform. There is no other use for the return value in Windows CE. The brevert parameter is ignored. Use the following code to disable the Close button:

EnableMenuItem (GetSystemMenu(hwnd, FALSE), SC_CLOSE,MF_BYCOMMAND | MF_GRAYED);
QuickInfo
  Windows NT: Requires version 3.1 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Requires version 1.0 or later.
  Header: Declared in winuser.h.
  Import Library: Use user32.lib.

See Also
Menus Overview, Menu Functions, GetMenu, WM_INITMENU, WM_SYSCOMMAND 

 


能不能給點有用的建議,說明檔案我也能找到。

uj5u.com熱心網友回復:

int CxxxxApp::ExitInstance() 

uj5u.com熱心網友回復:

參考 12 樓 schlafenhamster 的回復:
int CxxxxApp::ExitInstance() 

我查了下,這段代碼我用不上,我就直接給刪了。

uj5u.com熱心網友回復:

int CxxxxApp::ExitInstance() 
{
     if (pShellManager != NULL)
    {
          delete pShellManager;
    }
}

uj5u.com熱心網友回復:

我也遇到了一模一樣的問題. 感覺 CString 只要用了,立馬就會有記憶體泄漏,根本不知道是什么原因,現在都不敢用 CString 了.
樓主是哪里人? 

uj5u.com熱心網友回復:

參考 14 樓 schlafenhamster 的回復:
int CxxxxApp::ExitInstance() 
{
     if (pShellManager != NULL)
    {
          delete pShellManager;
    }
}

pShellManager 是Instance函式的區域變數,不是全域的。這么寫不合適,即使是全域的,我也打算把這個刪了,我用不到。我記得這個好像是VS2005開始加進來的,以前都沒有。

uj5u.com熱心網友回復:

參考 15 樓 Karim_Benzema 的回復:
我也遇到了一模一樣的問題. 感覺 CString 只要用了,立馬就會有記憶體泄漏,根本不知道是什么原因,現在都不敢用 CString 了.
樓主是哪里人? 
.
我用了好多。。。。。。你這么說,我以后編程也不敢用了。是不是string比較安全?

uj5u.com熱心網友回復:

參考 17 樓 qq_34859917 的回復:
Quote: 參考 15 樓 Karim_Benzema 的回復:

我也遇到了一模一樣的問題. 感覺 CString 只要用了,立馬就會有記憶體泄漏,根本不知道是什么原因,現在都不敢用 CString 了.
樓主是哪里人? 
.
我用了好多。。。。。。你這么說,我以后編程也不敢用了。是不是string比較安全?


參考 17 樓 qq_34859917 的回復:
Quote: 參考 15 樓 Karim_Benzema 的回復:

我也遇到了一模一樣的問題. 感覺 CString 只要用了,立馬就會有記憶體泄漏,根本不知道是什么原因,現在都不敢用 CString 了.
樓主是哪里人? 
.
我用了好多。。。。。。你這么說,我以后編程也不敢用了。是不是string比較安全?

他在瞎說,CString跟記憶體泄漏沒有半毛錢關系,功夫不到家怪編譯器

uj5u.com熱心網友回復:

參考 18 樓 rxguoblp 的回復:
Quote: 參考 17 樓 qq_34859917 的回復:

Quote: 參考 15 樓 Karim_Benzema 的回復:

我也遇到了一模一樣的問題. 感覺 CString 只要用了,立馬就會有記憶體泄漏,根本不知道是什么原因,現在都不敢用 CString 了.
樓主是哪里人? 
.
我用了好多。。。。。。你這么說,我以后編程也不敢用了。是不是string比較安全?

他在瞎說,CString跟記憶體泄漏沒有半毛錢關系,功夫不到家怪編譯器

好吧,我一直沒找到是什么原因導致了CString記憶體泄露。

uj5u.com熱心網友回復:

參考 18 樓 rxguoblp 的回復:
Quote: 參考 17 樓 qq_34859917 的回復:

Quote: 參考 15 樓 Karim_Benzema 的回復:

我也遇到了一模一樣的問題. 感覺 CString 只要用了,立馬就會有記憶體泄漏,根本不知道是什么原因,現在都不敢用 CString 了.
樓主是哪里人? 
.
我用了好多。。。。。。你這么說,我以后編程也不敢用了。是不是string比較安全?


參考 17 樓 qq_34859917 的回復:
Quote: 參考 15 樓 Karim_Benzema 的回復:

我也遇到了一模一樣的問題. 感覺 CString 只要用了,立馬就會有記憶體泄漏,根本不知道是什么原因,現在都不敢用 CString 了.
樓主是哪里人? 
.
我用了好多。。。。。。你這么說,我以后編程也不敢用了。是不是string比較安全?

他在瞎說,CString跟記憶體泄漏沒有半毛錢關系,功夫不到家怪編譯器


我知道不是CString引起的, 但現象跟樓主的一樣, 肯定是某一個地方有漏洞,但究竟是哪里出錯了,以我目前的三腳貓功夫, 是真的不知道, 也查不出來.

uj5u.com熱心網友回復:

"不是CString引起的" 你的 vc 版本可能有問題

uj5u.com熱心網友回復:

new出來的東西初始化了嗎?

uj5u.com熱心網友回復:

CString使用不當確實是會導致記憶體泄漏問題~

uj5u.com熱心網友回復:

參考 21 樓 schlafenhamster 的回復:
"不是CString引起的" 你的 vc 版本可能有問題
3
VS2010,你是說安裝程序有問題?

uj5u.com熱心網友回復:

參考 22 樓 qq_22675143 的回復:
new出來的東西初始化了嗎?
3
初始化了,而且都delete了。

uj5u.com熱心網友回復:

參考 23 樓 VisualEleven 的回復:
CString使用不當確實是會導致記憶體泄漏問題~

我覺得是CString的問題,但是不清楚是什么原因引起的。

uj5u.com熱心網友回復:

對這種問題一直沒辦法,進來學習下

uj5u.com熱心網友回復:

還沒有解決嗎? 
排除你的 CString 外, 檢查是否 關閉程式后,子執行緒未正確退出引出? 這樣的記憶體泄露也會反饋到  strcore.cpp 上。
另外,根據你的第一個 wincore.cpp 報告泄漏,也懷疑你的程式中有 非模態對話框沒有正常的退出。 檢查一下邏輯關系。
==
這樣的問題比較復雜,在有限的條件 下只能這樣提供思路。

uj5u.com熱心網友回復:

參考 27 樓 zcnc2012 的回復:
對這種問題一直沒辦法,進來學習下

還沒解決呢。

uj5u.com熱心網友回復:

參考 28 樓 easunlee 的回復:
還沒有解決嗎? 
排除你的 CString 外, 檢查是否 關閉程式后,子執行緒未正確退出引出? 這樣的記憶體泄露也會反饋到  strcore.cpp 上。
另外,根據你的第一個 wincore.cpp 報告泄漏,也懷疑你的程式中有 非模態對話框沒有正常的退出。 檢查一下邏輯關系。
==
這樣的問題比較復雜,在有限的條件 下只能這樣提供思路。

有用,多謝提醒,我用OpenGL在對話框上開了個視窗。但是注釋掉那段代碼后還是會出現記憶體泄漏。

uj5u.com熱心網友回復:

進來看這個貼,居然我也遇到相同的問題了。
是什么原因呢?

uj5u.com熱心網友回復:

快速找到記憶體泄漏
單確定了記憶體泄漏發生在哪一行,有時候并不足夠。特別是同一個new對應有多處釋放的情形。在實際的工程中,以下兩種情況很典型:

創建物件的地方是一個類工廠(ClassFactory)模式。很多甚至全部類實體由同一個new創建。對于此,定位到了new出物件的所在行基本沒有多大幫助。
 
COM物件。我們知道COM物件采用Reference Count維護生命周期。也就是說,物件new的地方只有一個,但是Release的地方很多,你要一個個排除。
那么,有什么好辦法,可以迅速定位記憶體泄漏?

答:有。

在記憶體泄漏情況復雜的時候,你可以用以下方法定位記憶體泄漏。這是我個人認為通用的記憶體泄漏追蹤方法中最有效的手段。

我們再回頭看看crtdbg生成的記憶體泄漏報告:

Detected memory leaks!
Dumping objects ->
c:\work\test.cpp(186) : {52} normal block at 0x003C4410, 40 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
除了產生該記憶體泄漏的記憶體分配陳述句所在的檔案名、行號為,我們注意到有一個比較陌生的資訊:{52}。這個整數值代表了什么意思呢?

其實,它代表了第幾次記憶體分配操作。象這個例子,{52}代表了第52次記憶體分配操作發生了泄漏。你可能要說,我只new過一次,怎么會是第52次?這很容易理解,其他的記憶體申請操作在C的初始化程序呼叫的唄。:)

有沒有可能,我們讓程式運行到第52次記憶體分配操作的時候,自動停下來,進入除錯狀態?所幸,crtdbg確實提供了這樣的函式:即 long _CrtSetBreakAlloc(long nAllocID)。我們加上它:

inline void EnableMemLeakCheck()
{
   _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}

#ifdef _DEBUG
#define new   new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

void main()
{
   EnableMemLeakCheck();
   _CrtSetBreakAlloc(52);
   int* leak = new int[10];
}
你發現,程式運行到 int* leak = new int[10]; 一句時,自動停下來進入除錯狀態。細細體會一下,你可以發現,這種方式你獲得的資訊遠比在程式退出時獲得檔案名及行號有價值得多。因為報告泄漏檔案名及行號,你獲得的只是靜態的資訊,然而_CrtSetBreakAlloc則是把整個現場恢復,你可以通過對函式呼叫堆疊分析(我發現很多人不習慣看函式呼叫堆疊,如果你屬于這種情況,我強烈推薦你去補上這一課,因為它太重要了)以及其他在線除錯技巧,來分析產生記憶體泄漏的原因。通常情況下,這種分析方法可以在5分鐘內找到肇事者。

當然,_CrtSetBreakAlloc要求你的程式執行程序是可還原的(多次執行程序的記憶體分配順序不會發生變化)。這個假設在多數情況下成立。不過,在多執行緒的情況下,這一點有時難以保證。

uj5u.com熱心網友回復:

記憶體泄漏報告,很多場合是參考依賴庫造成的假象。可以在連接器-依賴項-中延遲加載的dll中設定依賴的dll,這樣基本上不會虛報

uj5u.com熱心網友回復:

我個人覺得:應該是你的對話框沒有正確退出(或者對話框退出時沒有釋放對話框所擁有的資源)才導致的你的這個記憶體泄漏。

把排查重點放到你的CECT1Dlg類中,不要在App中做無用功了

uj5u.com熱心網友回復:

學習了,謝謝

uj5u.com熱心網友回復:

我最近也發現了,自己new的東西都delete了,但是就是報記憶體泄漏,而且都是系統自己生成的代碼造成的。不知道咋回事

uj5u.com熱心網友回復:



f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp(4500) : {563} client block at 0x0184A3C8, subtype c0, 56 bytes long.
a CObject object at $0184A3C8, 56 bytes long
{548} normal block at 0x01849438, 40 bytes long.
 Data: <8   8   8       > 38 94 84 01 38 94 84 01 38 94 84 01 CD CD CD CD 
{547} normal block at 0x018492B8, 8 bytes long.
 Data: <  I     > D4 FD 49 00 00 00 00 00 
其實是下面這個地方記憶體泄漏引起上面的:f:\dd\......
d:\vs2010\ect1\ect1\ect1.cpp(59) : {544} client block at 0x01849210, subtype c0, 12 bytes long.
不要說“自己的代碼絕對沒問題“,”絕對都釋放了之類的話“,必定是你的代碼有問題,再好好檢查下吧

uj5u.com熱心網友回復:

泄露的資訊里不寫了具體哪一行有問題了嗎,貼出對應的代碼來看看

uj5u.com熱心網友回復:

導致記憶體泄漏的原因有好多呢。。。不光new、delete,還有gdi泄漏啊什么的,我覺得你可以先運行程式然后查看任務管理器里的記憶體變化情況,或者你百度一個SystemInfo,這個東西可以幫你看程式運行時的記憶體變化情況,如果它一直升高,那肯定有問題。
然后你的程式中有沒有用到第三方,你看看你的debug下面有沒有一些可疑的dll,如果有些可以刪掉的話而不影響程式運行的話,那就刪掉然后再看記憶體變化情況。
最后你這個是VLD監測嗎,這個東西還是不敢恭維啊。。。。

uj5u.com熱心網友回復:

f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156)   
這種CString的泄露,   99%都是在執行緒中使用了CString物件, 然后容易報這個記憶體泄露.

估計是你的程式沒有正常結束, 也就是說結束是有些地方崩潰了,

uj5u.com熱心網友回復:

參考 31 樓 Anoldog 的回復:
進來看這個貼,居然我也遇到相同的問題了。
是什么原因呢?


代碼發到[email protected]我給你看看,不要懷疑mfc,記憶體泄漏問題的定位有那么復雜嘛

uj5u.com熱心網友回復:

11點以前,過時不候!

uj5u.com熱心網友回復:

   _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);

uj5u.com熱心網友回復:

使用CString::GetBuffer后要記得ReleaseBuffer

uj5u.com熱心網友回復:

參考 44 樓 u011330815 的回復:
使用CString::GetBuffer后要記得ReleaseBuffer

長了姿勢。。。從來不記得releaseBuffer

uj5u.com熱心網友回復:

虎頭蛇尾是俗人的習慣,
有始有終是君子的操守。

uj5u.com熱心網友回復:

http://www.cnblogs.com/skynet/archive/2011/02/20/1959162.html  你可以嘗試一下。我感覺不是CString出問題,而是你用的有問題。你要仔細查看代碼,確保new的一定被delete掉了,還有一些需要呼叫release的

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/102570.html

標籤:界面

上一篇:mfc 怎么用包含頭檔案的方式呼叫這個頭檔案的方法

下一篇:ListCtrl控制元件雙擊后無法獲取除第1列以外的行號

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • Git本地庫既關聯GitHub又關聯Gitee

    創建代碼倉庫 使用gitee舉例(github和gitee差不多) 1.在gitee右上角點擊+,選擇新建倉庫 ? 2.選擇填寫倉庫資訊,然后進行創建 ? 3.服務端已經準備好了,本地開始作準備 (1)Git 全域設定 git config --global user.name "成鈺" git c ......

    uj5u.com 2020-09-10 05:04:14 more
  • CODING DevOps 代碼質量實戰系列第二課,相約周三

    隨著 ToB(企業服務)的興起和 ToC(消費互聯網)產品進入成熟期,線上故障帶來的損失越來越大,代碼質量越來越重要,而「質量內建」正是 DevOps 核心理念之一。**《DevOps 代碼質量實戰(PHP 版)》**為 CODING DevOps 代碼質量實戰系列的第二課,同時也是本系列的 PHP ......

    uj5u.com 2020-09-10 05:07:43 more
  • 推薦Scrum書籍

    推薦Scrum書籍 直接上干貨,推薦書籍清單如下(推薦有順序的哦) Scrum指南 Scrum精髓 Scrum敏捷軟體開發 Scrum捷徑 硝煙中的Scrum和XP : 我們如何實施Scrum 敏捷軟體開發:Scrum實戰指南 Scrum要素 大規模Scrum:大規模敏捷組織的設計 用戶故事地圖 用 ......

    uj5u.com 2020-09-10 05:07:45 more
  • CODING DevOps 代碼質量實戰系列最后一課,周四發車

    隨著 ToB(企業服務)的興起和 ToC(消費互聯網)產品進入成熟期,線上故障帶來的損失越來越大,代碼質量越來越重要,而「質量內建」正是 DevOps 核心理念之一。 **《DevOps 代碼質量實戰(Java 版)》**為 CODING DevOps 代碼質量實戰系列的最后一課,同時也是本系列的 ......

    uj5u.com 2020-09-10 05:07:52 more
  • 敏捷軟體工程實踐書籍

    Scrum轉型想要做好,第一步先了解并真正落實Scrum,那么我推薦的Scrum書籍是要看懂并實踐的。第二步是團隊的工程實踐要做扎實。 下面推薦工程實踐書單: 重構:改善既有代碼的設計 決議極限編程 : 擁抱變化 代碼整潔代碼 程式員的職業素養 修改代碼的藝術 撰寫可讀代碼的藝術 測驗驅動開發 : ......

    uj5u.com 2020-09-10 05:07:55 more
  • Jenkins+svn+nginx實作windows環境自動部署vue前端專案

    前面文章介紹了Jenkins+svn+tomcat實作自動化部署,現在終于有空抽時間出來寫下Jenkins+svn+nginx實作自動部署vue前端專案。 jenkins的安裝和配置已經在前面文章進行介紹,下面介紹實作vue前端專案需要進行的哪些額外的步驟。 注意:在安裝jenkins和nginx的 ......

    uj5u.com 2020-09-10 05:08:49 more
  • CODING DevOps 微服務專案實戰系列第一課,明天等你

    CODING DevOps 微服務專案實戰系列第一課**《DevOps 微服務專案實戰:DevOps 初體驗》**將由 CODING DevOps 開發工程師 王寬老師 向大家介紹 DevOps 的基本理念,并探討為什么現代開發活動需要 DevOps,同時將以 eShopOnContainers 項 ......

    uj5u.com 2020-09-10 05:09:14 more
  • CODING DevOps 微服務專案實戰系列第二課來啦!

    近年來,工程專案的結構越來越復雜,需要接入合適的持續集成流水線形式,才能滿足更多變的需求,那么如何優雅地使用 CI 能力提升生產效率呢?CODING DevOps 微服務專案實戰系列第二課 《DevOps 微服務專案實戰:CI 進階用法》 將由 CODING DevOps 全堆疊工程師 何晨哲老師 向 ......

    uj5u.com 2020-09-10 05:09:33 more
  • CODING DevOps 微服務專案實戰系列最后一課,周四開講!

    隨著軟體工程越來越復雜化,如何在 Kubernetes 集群進行灰度發布成為了生產部署的”必修課“,而如何實作安全可控、自動化的灰度發布也成為了持續部署重點關注的問題。CODING DevOps 微服務專案實戰系列最后一課:**《DevOps 微服務專案實戰:基于 Nginx-ingress 的自動 ......

    uj5u.com 2020-09-10 05:10:00 more
  • CODING 儀表盤功能正式推出,實作作業資料可視化!

    CODING 儀表盤功能現已正式推出!該功能旨在用一張張統計卡片的形式,統計并展示使用 CODING 中所產生的資料。這意味著無需額外的設定,就可以收集歸納寶貴的作業資料并予之量化分析。這些海量的資料皆會以圖表或串列的方式躍然紙上,方便團隊成員隨時查看各專案的進度、狀態和指標,云端協作迎來真正意義上 ......

    uj5u.com 2020-09-10 05:11:01 more
最新发布
  • windows系統git使用ssh方式和gitee/github進行同步

    使用git來clone專案有兩種方式:HTTPS和SSH:
    HTTPS:不管是誰,拿到url隨便clone,但是在push的時候需要驗證用戶名和密碼;
    SSH:clone的專案你必須是擁有者或者管理員,而且需要在clone前添加SSH Key。SSH 在push的時候,是不需要輸入用戶名的,如果配置... ......

    uj5u.com 2023-04-19 08:41:12 more
  • windows系統git使用ssh方式和gitee/github進行同步

    使用git來clone專案有兩種方式:HTTPS和SSH:
    HTTPS:不管是誰,拿到url隨便clone,但是在push的時候需要驗證用戶名和密碼;
    SSH:clone的專案你必須是擁有者或者管理員,而且需要在clone前添加SSH Key。SSH 在push的時候,是不需要輸入用戶名的,如果配置... ......

    uj5u.com 2023-04-19 08:35:34 more
  • 2023年農牧行業6大CRM系統、5大場景盤點

    在物聯網、大資料、云計算、人工智能、自動化技術等現代資訊技術蓬勃發展與逐步成熟的背景下,數字化正成為農牧行業供給側結構性變革與高質量發展的核心驅動因素。因此,改造和提升傳統農牧業、開拓創新現代智慧農牧業,加快推進農牧業的現代化、資訊化、數字化建設已成為農牧業發展的重要方向。 當下,企業數字化轉型已經 ......

    uj5u.com 2023-04-18 08:05:44 more
  • 2023年農牧行業6大CRM系統、5大場景盤點

    在物聯網、大資料、云計算、人工智能、自動化技術等現代資訊技術蓬勃發展與逐步成熟的背景下,數字化正成為農牧行業供給側結構性變革與高質量發展的核心驅動因素。因此,改造和提升傳統農牧業、開拓創新現代智慧農牧業,加快推進農牧業的現代化、資訊化、數字化建設已成為農牧業發展的重要方向。 當下,企業數字化轉型已經 ......

    uj5u.com 2023-04-18 08:00:18 more
  • 計算機組成原理—存盤器

    計算機組成原理—硬體結構 二、存盤器 1.概述 存盤器是計算機系統中的記憶設備,用來存放程式和資料 1.1存盤器的層次結構 快取-主存層次主要解決CPU和主存速度不匹配的問題,速度接近快取 主存-輔存層次主要解決存盤系統的容量問題,容量接近與價位接近于主存 2.主存盤器 2.1概述 主存與CPU的聯 ......

    uj5u.com 2023-04-17 08:20:31 more
  • 談一談我對協同開發的一些認識

    如今各互聯網公司普通都使用敏捷開發,采用小步快跑的形式來進行專案開發。如果是小專案或者小需求,那一個開發可能就搞定了。但對于電商等復雜的系統,其功能多,結構復雜,一個人肯定是搞不定的,所以都是很多人來共同開發維護。以我曾經待過的商城團隊為例,光是后端開發就有七十多人。 為了更好地開發這類大型系統,往 ......

    uj5u.com 2023-04-17 08:18:55 more
  • 專案管理PRINCE2核心知識點整理

    PRINCE2,即 PRoject IN Controlled Environment(受控環境中的專案)是一種結構化的專案管理方法論,由英國政府內閣商務部(OGC)推出,是英國專案管理標準。
    PRINCE2 作為一種開放的方法論,是一套結構化的專案管理流程,描述了如何以一種邏輯性的、有組織的方法,... ......

    uj5u.com 2023-04-17 08:18:51 more
  • 談一談我對協同開發的一些認識

    如今各互聯網公司普通都使用敏捷開發,采用小步快跑的形式來進行專案開發。如果是小專案或者小需求,那一個開發可能就搞定了。但對于電商等復雜的系統,其功能多,結構復雜,一個人肯定是搞不定的,所以都是很多人來共同開發維護。以我曾經待過的商城團隊為例,光是后端開發就有七十多人。 為了更好地開發這類大型系統,往 ......

    uj5u.com 2023-04-17 08:18:00 more
  • 專案管理PRINCE2核心知識點整理

    PRINCE2,即 PRoject IN Controlled Environment(受控環境中的專案)是一種結構化的專案管理方法論,由英國政府內閣商務部(OGC)推出,是英國專案管理標準。
    PRINCE2 作為一種開放的方法論,是一套結構化的專案管理流程,描述了如何以一種邏輯性的、有組織的方法,... ......

    uj5u.com 2023-04-17 08:17:55 more
  • 計算機組成原理—存盤器

    計算機組成原理—硬體結構 二、存盤器 1.概述 存盤器是計算機系統中的記憶設備,用來存放程式和資料 1.1存盤器的層次結構 快取-主存層次主要解決CPU和主存速度不匹配的問題,速度接近快取 主存-輔存層次主要解決存盤系統的容量問題,容量接近與價位接近于主存 2.主存盤器 2.1概述 主存與CPU的聯 ......

    uj5u.com 2023-04-17 08:12:06 more