Discussion:
WMI not releasing memry
(too old to reply)
s***@patni.com
2007-03-09 12:44:44 UTC
Permalink
The code i have given below is not releasing memory,plz help me
because wmi using 1MB memry space.

bool CWirelessUploader::GetWMIAccessPointMACAddress( void )
{
bool bReturn = false;
DWORD dwErrorCode = CONST_0;
IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;
IWbemClassObject * pClassObject = NULL;
HRESULT hRes ;
BSTR bstrNamespace = (NAMESPACE);
BSTR strQuery = (QUERY_MACADDRESS);
BSTR strQL = (QUERY_LANG);
ULONG uCount = CONST_1;
ULONG uReturned;
CString strTempMac = EMPTY_STRING;
CString strNewMacAddress = EMPTY_STRING;
BSTR strClassProp = (MAC_PROPERTY);
CString strSendLogMsg = EMPTY_STRING ;
CString strWriteLogMsg = EMPTY_STRING;
//ms_pMacAddress = new char [CONST_12 + 1];
CoInitialize(NULL);
hRes = CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0
);
if( hRes == S_OK || hRes == RPC_E_TOO_LATE )
{
if(CoCreateInstance (CLSID_WbemAdministrativeLocator,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
IID_IUnknown ,
( void ** ) &pIWbemLocator
) == S_OK)
{
if(pIWbemLocator->ConnectServer(bstrNamespace, // Namespace
NULL, // Userid
NULL, // PW
NULL, // Locale
CONST_0, // flags
NULL, // Authority
NULL, // Context
&pWbemServices
) == S_OK)
{
if(pWbemServices->ExecQuery(strQL, strQuery,
WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject) == S_OK)
{
if(pEnumObject->Reset() == S_OK)
{
while(pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject,
&uReturned) == WBEM_S_NO_ERROR)
{
VARIANT vVal;
//VariantInit(&vVal);
hRes = pClassObject->Get(strClassProp, CONST_0, &vVal, 0, 0);
if(hRes != S_OK)
{
VariantClear( &vVal );
bReturn = false;
strSendLogMsg = "pClassObject->Get called failed %d";
SendLogMessage(strSendLogMsg, dwErrorCode, true,
ERROR_LOG_LEVEL_1);
strSendLogMsg.Empty();
}
else
{
long lLowerBound, lUpperBound;
int iMacAdd[CONST_6] = {CONST_0 };
strTempMac = EMPTY_STRING;
SafeArrayGetLBound(vVal.parray, CONST_1, &lLowerBound);
SafeArrayGetUBound(vVal.parray, CONST_1, &lUpperBound);
strNewMacAddress.Empty();
strNewMacAddress = EMPTY_STRING;
for (lLowerBound; lLowerBound <= lUpperBound; lLowerBound++)
{
strTempMac.Empty();
SafeArrayGetElement(vVal.parray, &lLowerBound,
&iMacAdd[lLowerBound]);
strTempMac.Format(HEX_FORMAT ,iMacAdd[lLowerBound] );
strNewMacAddress.Append(strTempMac);
}
strNewMacAddress = strNewMacAddress.MakeUpper();
strTempMac.Empty();
}
ULONG uReturnCount;
uReturnCount = pClassObject->Release();
strSendLogMsg.Format("Return class object count is %d",
uReturnCount);
SendLogMessage(strSendLogMsg, dwErrorCode, true,
ERROR_LOG_LEVEL_1);
pClassObject = NULL;
VariantClear( &vVal );

}
}
ULONG uReturnCount;
uReturnCount = pEnumObject->Release();
strSendLogMsg.Format("Return Enum object count is %d",
uReturnCount);
SendLogMessage(strSendLogMsg, dwErrorCode, true,
ERROR_LOG_LEVEL_1);
}
ULONG uReturnCount;
uReturnCount = pWbemServices->Release();
strSendLogMsg.Format("Return Service object count is %d",
uReturnCount);
SendLogMessage(strSendLogMsg, dwErrorCode, true,
ERROR_LOG_LEVEL_1);
pWbemServices = NULL;
}
ULONG uReturnCount;
uReturnCount = pIWbemLocator->Release();
strSendLogMsg.Format("Return Locator object count is %d",
uReturnCount);
SendLogMessage(strSendLogMsg, dwErrorCode, true,
ERROR_LOG_LEVEL_1);
pIWbemLocator = NULL;
}
}
if( ms_pMacAddress != NULL && strNewMacAddress != EMPTY_STRING )
{
memcpy(ms_pMacAddress, strNewMacAddress, CONST_13 );
if(hEngRscDLL != NULL)
{
strSendLogMsg.LoadString( hEngRscDLL, IDS_UL_MAC_ADDRESS );
}
strWriteLogMsg.Format(strSendLogMsg, strNewMacAddress);
SendLogMessage(strWriteLogMsg, dwErrorCode, false,
ERROR_LOG_LEVEL_1);
strSendLogMsg.Empty();
bReturn = true;
}
else
{
bReturn = false;
}
if(pIWbemLocator != NULL)
{
pIWbemLocator->Release();
pIWbemLocator = NULL;
}
if(pWbemServices != NULL)
{
pWbemServices->Release();
pWbemServices = NULL;
}
if(pEnumObject != NULL)
{
pEnumObject->Release();
pEnumObject = NULL;
}
strNewMacAddress.Empty();
strSendLogMsg.Empty();
strTempMac.Empty();
strWriteLogMsg.Empty();
CoUninitialize();
//system("TaskKill /F /IM wmiprvse.exe");
return bReturn;
}
Richard T. Edwards
2007-03-23 03:13:51 UTC
Permalink
It looks like you're cleaning everything up at the end of this code as it
should be done.

Which header file are you using?
Post by s***@patni.com
The code i have given below is not releasing memory,plz help me
because wmi using 1MB memry space.
bool CWirelessUploader::GetWMIAccessPointMACAddress( void )
{
bool bReturn = false;
DWORD dwErrorCode = CONST_0;
IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;
IWbemClassObject * pClassObject = NULL;
HRESULT hRes ;
BSTR bstrNamespace = (NAMESPACE);
BSTR strQuery = (QUERY_MACADDRESS);
BSTR strQL = (QUERY_LANG);
ULONG uCount = CONST_1;
ULONG uReturned;
CString strTempMac = EMPTY_STRING;
CString strNewMacAddress = EMPTY_STRING;
BSTR strClassProp = (MAC_PROPERTY);
CString strSendLogMsg = EMPTY_STRING ;
CString strWriteLogMsg = EMPTY_STRING;
//ms_pMacAddress = new char [CONST_12 + 1];
CoInitialize(NULL);
hRes = CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0
);
if( hRes == S_OK || hRes == RPC_E_TOO_LATE )
{
if(CoCreateInstance (CLSID_WbemAdministrativeLocator,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
IID_IUnknown ,
( void ** ) &pIWbemLocator
) == S_OK)
{
if(pIWbemLocator->ConnectServer(bstrNamespace, // Namespace
NULL, // Userid
NULL, // PW
NULL, // Locale
CONST_0, // flags
NULL, // Authority
NULL, // Context
&pWbemServices
) == S_OK)
{
if(pWbemServices->ExecQuery(strQL, strQuery,
WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject) == S_OK)
{
if(pEnumObject->Reset() == S_OK)
{
while(pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject,
&uReturned) == WBEM_S_NO_ERROR)
{
VARIANT vVal;
//VariantInit(&vVal);
hRes = pClassObject->Get(strClassProp, CONST_0, &vVal, 0, 0);
if(hRes != S_OK)
{
VariantClear( &vVal );
bReturn = false;
strSendLogMsg = "pClassObject->Get called failed %d";
SendLogMessage(strSendLogMsg, dwErrorCode, true,
ERROR_LOG_LEVEL_1);
strSendLogMsg.Empty();
}
else
{
long lLowerBound, lUpperBound;
int iMacAdd[CONST_6] = {CONST_0 };
strTempMac = EMPTY_STRING;
SafeArrayGetLBound(vVal.parray, CONST_1, &lLowerBound);
SafeArrayGetUBound(vVal.parray, CONST_1, &lUpperBound);
strNewMacAddress.Empty();
strNewMacAddress = EMPTY_STRING;
for (lLowerBound; lLowerBound <= lUpperBound; lLowerBound++)
{
strTempMac.Empty();
SafeArrayGetElement(vVal.parray, &lLowerBound,
&iMacAdd[lLowerBound]);
strTempMac.Format(HEX_FORMAT ,iMacAdd[lLowerBound] );
strNewMacAddress.Append(strTempMac);
}
strNewMacAddress = strNewMacAddress.MakeUpper();
strTempMac.Empty();
}
ULONG uReturnCount;
uReturnCount = pClassObject->Release();
strSendLogMsg.Format("Return class object count is %d",
uReturnCount);
SendLogMessage(strSendLogMsg, dwErrorCode, true,
ERROR_LOG_LEVEL_1);
pClassObject = NULL;
VariantClear( &vVal );
}
}
ULONG uReturnCount;
uReturnCount = pEnumObject->Release();
strSendLogMsg.Format("Return Enum object count is %d",
uReturnCount);
SendLogMessage(strSendLogMsg, dwErrorCode, true,
ERROR_LOG_LEVEL_1);
}
ULONG uReturnCount;
uReturnCount = pWbemServices->Release();
strSendLogMsg.Format("Return Service object count is %d",
uReturnCount);
SendLogMessage(strSendLogMsg, dwErrorCode, true,
ERROR_LOG_LEVEL_1);
pWbemServices = NULL;
}
ULONG uReturnCount;
uReturnCount = pIWbemLocator->Release();
strSendLogMsg.Format("Return Locator object count is %d",
uReturnCount);
SendLogMessage(strSendLogMsg, dwErrorCode, true,
ERROR_LOG_LEVEL_1);
pIWbemLocator = NULL;
}
}
if( ms_pMacAddress != NULL && strNewMacAddress != EMPTY_STRING )
{
memcpy(ms_pMacAddress, strNewMacAddress, CONST_13 );
if(hEngRscDLL != NULL)
{
strSendLogMsg.LoadString( hEngRscDLL, IDS_UL_MAC_ADDRESS );
}
strWriteLogMsg.Format(strSendLogMsg, strNewMacAddress);
SendLogMessage(strWriteLogMsg, dwErrorCode, false,
ERROR_LOG_LEVEL_1);
strSendLogMsg.Empty();
bReturn = true;
}
else
{
bReturn = false;
}
if(pIWbemLocator != NULL)
{
pIWbemLocator->Release();
pIWbemLocator = NULL;
}
if(pWbemServices != NULL)
{
pWbemServices->Release();
pWbemServices = NULL;
}
if(pEnumObject != NULL)
{
pEnumObject->Release();
pEnumObject = NULL;
}
strNewMacAddress.Empty();
strSendLogMsg.Empty();
strTempMac.Empty();
strWriteLogMsg.Empty();
CoUninitialize();
//system("TaskKill /F /IM wmiprvse.exe");
return bReturn;
}
Loading...