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

📄 checkmemory.cpp

📁 查看是否有内存泄漏以及具体的泄漏值
💻 CPP
字号:
#include "stdafx.h"
#include <Windows.h>

MEMORYSTATUS gSystem_Memory_Start;
MEMORYSTATUS gSystem_Memory_End;

void CheckMemoryDriffInit(void)
{
	GlobalMemoryStatus(&gSystem_Memory_Start);
}

CString CheckMemoryDriff(void)
{
	 CString nstr;
	 long nAvailPhys;
	 long nAvailVirtual;
	
	 GlobalMemoryStatus(&gSystem_Memory_End);// memory status structure
	 nAvailPhys = gSystem_Memory_End.dwAvailPhys;
	 nAvailPhys -= gSystem_Memory_Start.dwAvailPhys;
	 nAvailVirtual = gSystem_Memory_End.dwAvailVirtual;
	 nAvailVirtual -= gSystem_Memory_Start.dwAvailVirtual;
	 
	 /*
	 nstr.Format(_T("物理内存剩余容量差 = %d Byte = %d KB = %d MB\n虚拟内存剩余容量差 = %d Byte = %d KB = %d MB\n"),
	  nAvailPhys,
	  nAvailPhys/1024,
	  nAvailPhys/1024/1024,
	  nAvailVirtual,
	  nAvailVirtual/1024,
	  nAvailVirtual/1024/1024);
	 //AfxMessageBox(nstr);
	 */
	 nstr.Format(_T("%d KB = %d MB   Sys=%d KB = %d MB"),nAvailPhys/1024,nAvailPhys/1024/1024,gSystem_Memory_End.dwAvailPhys/1024,gSystem_Memory_End.dwAvailPhys/1024/1024);
	 return nstr;
	 
}
#if 0
MEMORYSTATUS gSystem_Memory_Start;
MEMORYSTATUS gSystem_Memory_End;
int gSystem_Memory_Flag=0;
void CheckMemoryDriff(void)
{
	CString nstr;
	long nAvailPhys;
	long nAvailVirtual;
	if(0==gSystem_Memory_Flag)
	{
		GlobalMemoryStatus(&gSystem_Memory_Start);
		gSystem_Memory_Flag = 1;
	}
	GlobalMemoryStatus(&gSystem_Memory_End);// memory status structure
	nAvailPhys = gSystem_Memory_End.dwAvailPhys;
	nAvailPhys -= gSystem_Memory_Start.dwAvailPhys;
	nAvailVirtual = gSystem_Memory_End.dwAvailVirtual;
	nAvailVirtual -= gSystem_Memory_Start.dwAvailVirtual;
	
	
	nstr.Format(_T("物理内存剩余容量差 = %d Byte = %d KB = %d MB\n虚拟内存剩余容量差 = %d Byte = %d KB = %d MB\n"),
		nAvailPhys,
		nAvailPhys/1024,
		nAvailPhys/1024/1024,
		nAvailVirtual,
		nAvailVirtual/1024,
		nAvailVirtual/1024/1024);
	AfxMessageBox(nstr);
	
}

#endif

#if 0
	DWORD ntime = GetTickCount();
	file.WritePrivateProfileString(_T("BookList1"),_T("MyBook1"),_T("navylau111"),_T("\\NAND\\test1.ini"));
	/*
	file.WritePrivateProfileString(_T("BookList1"),_T("MyBook1"),_T("navylau1"),_T("\\NAND\\test1.ini"));
	file.WritePrivateProfileString(_T("BookList2"),_T("MyBook1"),_T("navylau2"),_T("\\NAND\\test1.ini"));
	file.WritePrivateProfileString(_T("BookList3"),_T("MyBook1"),_T("navylau3"),_T("\\NAND\\test1.ini"));
	file.WritePrivateProfileString(_T("BookList4"),_T("MyBook1"),_T("navylau4"),_T("\\NAND\\test1.ini"));
	file.WritePrivateProfileString(_T("BookList5"),_T("MyBook1"),_T("navylau5"),_T("\\NAND\\test1.ini"));
	file.WritePrivateProfileString(_T("BookList6"),_T("MyBook1"),_T("navylau6"),_T("\\NAND\\test1.ini"));
	file.WritePrivateProfileString(_T("BookList7"),_T("MyBook1"),_T("navylau7"),_T("\\NAND\\test1.ini"));
	file.WritePrivateProfileString(_T("BookList8"),_T("MyBook1"),_T("navylau8"),_T("\\NAND\\test1.ini"));
	file.WritePrivateProfileString(_T("BookList9"),_T("MyBook1"),_T("navylau9"),_T("\\NAND\\test1.ini"));
	file.WritePrivateProfileString(_T("BookList10"),_T("MyBook1"),_T("navylau10"),_T("\\NAND\\test1.ini"));
	*/
	ntime = GetTickCount()-ntime;
	cstr.Format(_T("%d"),ntime);
	MessageBox(cstr);

	ntime = GetTickCount();
	file.GetPrivateProfileString(_T("BookList"),_T("MyBook1"),_T(""), nReadBuffer[0], 20, _T("\\NAND\\test1.ini"));
	ntime = GetTickCount()-ntime;
	cstr.Format(_T("%d"),ntime);
	MessageBox(cstr);
	/*
	file.GetPrivateProfileString(_T("BookList1"),_T("MyBook1"),_T(""), nReadBuffer[1], 20, _T("\\NAND\\test1.ini"));
	file.GetPrivateProfileString(_T("BookList2"),_T("MyBook1"),_T(""), nReadBuffer[2], 20, _T("\\NAND\\test1.ini"));
	file.GetPrivateProfileString(_T("BookList3"),_T("MyBook1"),_T(""), nReadBuffer[3], 20, _T("\\NAND\\test1.ini"));
	file.GetPrivateProfileString(_T("BookList4"),_T("MyBook1"),_T(""), nReadBuffer[4], 20, _T("\\NAND\\test1.ini"));
	file.GetPrivateProfileString(_T("BookList5"),_T("MyBook1"),_T(""), nReadBuffer[5], 20, _T("\\NAND\\test1.ini"));
	file.GetPrivateProfileString(_T("BookList6"),_T("MyBook1"),_T(""), nReadBuffer[6], 20, _T("\\NAND\\test1.ini"));
	file.GetPrivateProfileString(_T("BookList7"),_T("MyBook1"),_T(""), nReadBuffer[7], 20, _T("\\NAND\\test1.ini"));
	file.GetPrivateProfileString(_T("BookList8"),_T("MyBook1"),_T(""), nReadBuffer[8], 20, _T("\\NAND\\test1.ini"));
	file.GetPrivateProfileString(_T("BookList9"),_T("MyBook1"),_T(""), nReadBuffer[9], 20, _T("\\NAND\\test1.ini"));
	file.GetPrivateProfileString(_T("BookList10"),_T("MyBook1"),_T(""), nReadBuffer[10], 20, _T("\\NAND\\test1.ini"));
	*/
#endif

#if 0
DWORD GetPrivateProfileString(
	LPCTSTR lpAppName,        // section name
	LPCTSTR lpKeyName,        // key name
	LPCTSTR lpDefault,        // default string
	LPTSTR lpReturnedString,  // destination buffer
	DWORD nSize,              // size of destination buffer
	LPCTSTR lpFileName        // initialization file name
	)
	{
		CFile nFile;
		FILE *npFile;
		DWORD nFileLength;

		LPTSTR npFileBuffer=NULL;
		DWORD nFileBufferSize;
		
		LPTSTR npLineBuffer=NULL;

		TCHAR *npf;
		CString nFileStr;
		DWORD nFileStrSize;
		CString nStr;
		//CString nStr_Old;
		
		BOOL nbResult;

		int nIndex;
		int nSectionStart_Index;
		int nSectionEnd_Index;
		int nAppNameStart_Index;
		int nAppNameEnd_Index;
		int nEqualMark_Index;
		int nKeyNameStart_Index;
		int nKeyNameEnd_Index;
		int nStringStart_Index;
		int nStringEnd_Index;
		
		int nAppName_Length;
		int nKeyName_Length;
		int nString_Length;

		DWORD nReturnValue;
		//DWORD ntime = GetTickCount();

		nbResult = nFile.Open(lpFileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite,NULL);//若文件不存在,则创建一个新文件
		if(!nbResult)
		{
			//AfxMessageBox(_T("access file fail!"));
			goto EXIT_FALSE;
		}
		
		nFileLength = nFile.GetLength();
		nFileBufferSize = nFileLength + APPNAME_MAXLENGTH+KEYNAME_MAXLENGTH+STRING_MAXLENGTH+10;
		if(nFileBufferSize>FILE_MAXBYTES) //限制最大文件
		{
			//AfxMessageBox(_T("file too large!"));
			nFile.Close();
			goto EXIT_FALSE;
		}
		nFile.Close();

		npFileBuffer = new TCHAR [nFileBufferSize];//characters
		if(NULL==npFileBuffer)
		{
			//AfxMessageBox(_T("not enough memory for npFileBuffer!"));
			goto EXIT_FALSE;
		}
		memset(npFileBuffer,0,nFileBufferSize*2);
		
		npLineBuffer = new TCHAR [LINEBUFFER_MAXLENGTH];
		if(NULL==npLineBuffer)
		{
			//AfxMessageBox(_T("not enough memory for npLineBuffer!"));
			goto EXIT_FALSE;
		}
		memset(npLineBuffer,0,LINEBUFFER_MAXLENGTH*2);

		if((npFile = _tfopen(lpFileName, _T("rt"))) == NULL)
		{
			//AfxMessageBox(_T("can not open the file!"));
			goto EXIT_FALSE;
		}

		//while(!feof(npFile))
		while(1)
		{
			npf=_fgetts(npLineBuffer,LINEBUFFER_MAXLENGTH,npFile);
			if(npf==NULL) break;
			_tcscat(npFileBuffer,npLineBuffer);	
		}
		fclose(npFile);
		
		nFileStr.Format(_T("%s"),npFileBuffer);
		nFileStrSize = nFileStr.GetLength();//real amount of file characters
		
		nStr.Format(_T("[%s]"),lpAppName);
		nSectionStart_Index = nFileStr.Find(nStr);//搜索段名
		if(-1==nAppNameStart_Index)
		{//没有搜索到段名
			goto EXIT_FALSE;
		}

		nStr.Format(_T("\n["));
		nSectionEnd_Index = nFileStr.Find(nStr,nAppNameStart_Index+2+nAppName_Length);//搜索下一个段名位置
		if(-1==nSectionEnd_Index)
		{//所搜索的段名处在文件的最末尾
			nSectionEnd_Index = nFileStrSize -1;
		}


		nStr.Format(_T("\n%s"),lpKeyName);
		nIndex = nFileStr.Find(nStr,nSectionStart_Index+2);
		if( (-1==nKeyNameStart_Index)||(nKeyNameStart_Index>nAppNameEnd_Index))
		{//搜索到段名,但没有搜索到键名
			goto EXIT_FALSE;
		}
		nKeyNameStart_Index++;

		nStringEnd_Index = nFileStr.Find(_T('\n'),nKeyNameStart_Index);
		if(-1 == nStringEnd_Index)
		{//在键名这一行上,没有找到回车符
			nStringEnd_Index = nSectionEnd_Index;
		}

		nEqualMark_Index = nFileStr.Find(_T('='),nKeyNameStart_Index);
		if((-1 == nEqualMark_Index)||(nEqualMark_Index>nStringEnd_Index))
		{//在键名这一行上,没有找到等号,则默认该键无键值
			goto EXIT_FALSE;	
		}
		nIndex = nEqualMark_Index+1;
		while(nIndex <= nStringEnd_Index)
		{
			if(npFileBuffer[nIndex] != _T(' ')) break;
			nIndex ++;
		}

		if(nIndex>nStringEnd_Index) goto EXIT_FALSE;//找不到内容
		if(_T('\n')==npFileBuffer[nIndex]) goto EXIT_FALSE;//只找到回车符
			
		nStringStart_Index = nIndex;
		
		if(nStringEnd_Index-nStringStart_Index+1>nSize)
		{
			_tcsncpy(lpReturnedString,npFileBuffer+nStringStart_Index,nSize-1);
			lpReturnedString[nSize-1]=_T('\0');
			nReturnValue= nSize-1;
		}
		else
		{
			_tcsncpy(lpReturnedString,npFileBuffer+nStringStart_Index,nStringEnd_Index-nStringStart_Index);
			lpReturnedString[nStringEnd_Index-nStringStart_Index]=_T('\0');
			nReturnValue = nStringEnd_Index-nStringStart_Index;
		}
		
	EXIT_TRUE:
		if(NULL!=npFileBuffer)
		{
			delete [] npFileBuffer;
			npFileBuffer = NULL;
		}

		if(NULL!=npLineBuffer)
		{
			delete [] npLineBuffer;
			npLineBuffer = NULL;
		}
		
		//ntime = GetTickCount() - ntime; 
		//nStr.Format(_T("time cost = %d\n"),ntime);
		//AfxMessageBox(nStr);
		
		return nReturnValue;	
		
	EXIT_FALSE:
		if(NULL!=npFileBuffer)
		{
			delete [] npFileBuffer;
			npFileBuffer = NULL;
		}

		if(NULL!=npLineBuffer)
		{
			delete [] npLineBuffer;
			npLineBuffer = NULL;
		}

		nStr.Format(_T("%s"),lpDefault);
		nReturnValue=nStr.GetLength();
		if(nReturnValue+1>nSize)
		{
			_tcsncpy(lpReturnedString,lpDefault,nSize-1);
			lpReturnedString[nSize-1]=_T('\0');
			return (DWORD)(nSize-1);
		}
		else
		{
			_tcscpy(lpReturnedString,lpDefault);
			return nReturnValue;
		}
			
	};
#endif

⌨️ 快捷键说明

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