⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memoryleakverifydlg.cpp

📁 WinCE5.0下用于测试软件是否存在内存泄漏的源码
💻 CPP
字号:
// MemoryLeakVerifyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Winbase.h"
#include "MemoryLeakVerify.h"
#include "MemoryLeakVerifyDlg.h"
#include <stdlib.h>         
#include <stdio.h>
#include <malloc.h>




#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

HANDLE	hMemLeakThread;
LPMEMORYSTATUS  lpBuffer;
/////////////////////////////////////////////////////////////////////////////
// CMemoryLeakVerifyDlg dialog

CMemoryLeakVerifyDlg::CMemoryLeakVerifyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMemoryLeakVerifyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMemoryLeakVerifyDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMemoryLeakVerifyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMemoryLeakVerifyDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMemoryLeakVerifyDlg, CDialog)
	//{{AFX_MSG_MAP(CMemoryLeakVerifyDlg)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(IDC_CLOSE, OnClose)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMemoryLeakVerifyDlg message handlers

BOOL CMemoryLeakVerifyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
//	MemoryLeakVerify();



	return TRUE;  // return TRUE  unless you set the focus to a control
}





DWORD __stdcall CMemoryLeakVerifyDlg::MemLeakThread(LPVOID lparam)
{

	CMemoryLeakVerifyDlg *pDlg = (CMemoryLeakVerifyDlg*)lparam;
	while(TRUE)
	{
		pDlg->MemoryLeakVerify();
		Sleep(2000);
	}
	return 1;
}

void CMemoryLeakVerifyDlg::MemoryLeakVerify()
{
//	LPSYSTEM_INFO	lpSystemInfo = 0;
	
	DWORD nBufferSize;
	nBufferSize = sizeof(MEMORYSTATUS);
	DWORD dwStartTick, dwIdleSt, dwStopTick, PercentIdle, dwIdleEd;

	lpBuffer = (MEMORYSTATUS*)malloc(nBufferSize);

	GlobalMemoryStatus(lpBuffer);

	RETAILMSG(1, (TEXT(">>>skyxu>>>dwLength=%u; dwMemoryLoad=%u; dwTotalPhys=%u; dwAvailPhys=%u; dwTotalPageFile=%u; dwAvailPageFile=%u;dwTotalVirtual=%u; dwAvailVirtual=%u \r\n"), 
		lpBuffer->dwLength, lpBuffer->dwMemoryLoad, lpBuffer->dwTotalPhys, lpBuffer->dwAvailPhys, lpBuffer->dwTotalPageFile, lpBuffer->dwAvailPageFile,lpBuffer->dwTotalVirtual, lpBuffer->dwAvailVirtual
		));
	
	dwStartTick = GetTickCount();
	dwIdleSt = GetIdleTime();
	//  Insert a call to the Sleep(sleep_time) function to allow idle time
	//  to accrue. An example of an appropriate sleep time is 1000 ms.
	Sleep(1000);
	dwStopTick = GetTickCount();
	dwIdleEd = GetIdleTime();
	PercentIdle = ((100*(dwIdleEd - dwIdleSt)) / (dwStopTick - dwStartTick));

	RETAILMSG(1, (TEXT("CpuIdlePerCent=%u; \r\n"), PercentIdle));

#if 0
	RETAILMSG(1, (TEXT(">>>skyxu>>>dwLength=%u; dwMemoryLoad=%u; dwTotalPhys=%u; dwAvailPhys=%u; dwTotalVirtual=%u; dwAvailVirtual=%u \r\n"), 
		lpBuffer->dwLength, lpBuffer->dwMemoryLoad, lpBuffer->dwTotalPhys, lpBuffer->dwAvailPhys, lpBuffer->dwTotalVirtual, lpBuffer->dwAvailVirtual
		));
#endif
}


void CMemoryLeakVerifyDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	DWORD	IDThread;
//	HANDLE	hMemLeakThread;
	
	hMemLeakThread = CreateThread(NULL,0,MemLeakThread,this,0,&IDThread);
	if(NULL == hMemLeakThread)
	{
		RETAILMSG(1, (TEXT(">>>SKYXU>>>Create MemLeakThread is fail \r\n")));
		CloseHandle(hMemLeakThread);
		return;
	}

//	MemoryLeakVerify();
}

void CMemoryLeakVerifyDlg::OnClose() 
{
	// TODO: Add your control notification handler code here
	FreeMemory();
	//DestroyWindow();
	OnCancel();
}

void CMemoryLeakVerifyDlg::FreeMemory()
{
	CloseHandle(hMemLeakThread);
	free(lpBuffer);
}



void CMemoryLeakVerifyDlg::OnOK() 
{
	// TODO: Add extra validation here
	FreeMemory();
	CDialog::OnOK();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -