Fixing Memory Leaks
-
Hi, I've written a DLL which modifies some of the Registry Entries. I've used RegOpenKeyEx method for opening and RegCloseKey for closing the registry keys. When I run this code many times in a loop it shows a memory leak and the virtual byte count increases in the performance monitor. I've taken all precautions to check for any key remaining open. I've tried a similar code in a EXE code and it works fine without any byte increase. Can somebody help me in this regard. Thanks and Regards, Abhishek. I hope this code would give you some idea. ************************************************** BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } BOOL _cdecl CheckClient::Run(TCHAR *szEventLogMessage, TCHAR *szAlarmMessage) { long lRetValue = 0; HKEY hkey = NULL; TCHAR szRegPath[100] = {0}; _tcscpy(szRegPath, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList")); lRetValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE,szRegPath,0,KEY_AL L_ACCESS,&hkey); if (lRetValue == ERROR_SUCCESS) { long lRetVal = 0; _tcscpy(szEventLogMessage, _T("Executed this code")); lRetVal = RegCloseKey(hkey); if (lRetVal == ERROR_SUCCESS) { _tcscat(szEventLogMessage, _T("Success in closing the key")); } hkey = NULL; } hkey = NULL; if (_tcscmp(szEventLogMessage,_T("")) == 0) _tcscpy(szEventLogMessage, _T("The method has been executed first time.")); else _tcscat(szEventLogMessage, _T("The method has been executed second time.")); return TRUE; } __________________
-
Hi, I've written a DLL which modifies some of the Registry Entries. I've used RegOpenKeyEx method for opening and RegCloseKey for closing the registry keys. When I run this code many times in a loop it shows a memory leak and the virtual byte count increases in the performance monitor. I've taken all precautions to check for any key remaining open. I've tried a similar code in a EXE code and it works fine without any byte increase. Can somebody help me in this regard. Thanks and Regards, Abhishek. I hope this code would give you some idea. ************************************************** BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } BOOL _cdecl CheckClient::Run(TCHAR *szEventLogMessage, TCHAR *szAlarmMessage) { long lRetValue = 0; HKEY hkey = NULL; TCHAR szRegPath[100] = {0}; _tcscpy(szRegPath, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList")); lRetValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE,szRegPath,0,KEY_AL L_ACCESS,&hkey); if (lRetValue == ERROR_SUCCESS) { long lRetVal = 0; _tcscpy(szEventLogMessage, _T("Executed this code")); lRetVal = RegCloseKey(hkey); if (lRetVal == ERROR_SUCCESS) { _tcscat(szEventLogMessage, _T("Success in closing the key")); } hkey = NULL; } hkey = NULL; if (_tcscmp(szEventLogMessage,_T("")) == 0) _tcscpy(szEventLogMessage, _T("The method has been executed first time.")); else _tcscat(szEventLogMessage, _T("The method has been executed second time.")); return TRUE; } __________________
The function seems not to leak. Can you post the code of the loop where you're calling
CheckClient::Run
? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo