none
内存释放不掉,进程占用内存一直增加,有哪位大牛能给看一下是什么问题么,感谢! RRS feed

  • 问题

  • #include "stdafx.h"
    
    
    #include <locale.h>
    
    #include <ActiveDS.h>
    
    #include <map>
    
    #define SAFE_RELEASE(p)		{ if(p) { p->Release(); p = NULL; } }
    
    #pragma comment(lib, "adsiid.lib")
    #pragma comment(lib, "activeds.lib")
    
    using namespace std;
    
    const WCHAR* g_pszW3SVC = L"IIS://localhost/W3SVC";
    
    class CWebPathMap : public map<BSTR, BSTR>
    {
    public:
    	CWebPathMap();
    	~CWebPathMap();
    
    	VOID	Destroy();
    	VOID	MoveData(CWebPathMap & dest);
    };
    
    typedef CWebPathMap::iterator WPM_ITERATOR;
    
    CWebPathMap g_lastWPM;
    
    CWebPathMap::CWebPathMap()
    {
    }
    
    CWebPathMap::~CWebPathMap()
    {
    	Destroy();
    }
    
    void CWebPathMap::Destroy()
    {
    	WPM_ITERATOR it = begin();
    
    	while (it != end())
    	{
    		SysFreeString(it->first);
    		SysFreeString(it->second);
    
    		it = erase(it);
    	}
    
    	//clear();
    }
    
    VOID CWebPathMap::MoveData(CWebPathMap & dest)
    {
    	dest.Destroy();
    
    	WPM_ITERATOR it = begin();
    	while (it != end())
    	{
    		dest.insert( CWebPathMap::value_type(it->first, it->second) );
    		it++;
    	}
    
    	clear();
    }
    
    
    BSTR GetWebsitePath(BSTR bstrADsPath)
    {
    	BSTR rtn = NULL;
    
    	HRESULT hr;
    	IADs * pADs = NULL;
    	hr = ADsGetObject(bstrADsPath, IID_IADs, (VOID **)&pADs);
    	if (FAILED(hr))
    		return NULL;
    
    	VARIANT var;
    	VariantInit(&var);
    
    	hr = pADs->Get(L"Path", &var);
    	SAFE_RELEASE(pADs);
    	if (FAILED(hr))
    		return NULL;
    	rtn = SysAllocString(var.bstrVal);
    
    	VariantClear(&var);
    
    	return rtn;
    }
    
    VOID AddToMap(BSTR bstrChild, CWebPathMap& wpm)
    {
    	BSTR bstrWebSiteName = NULL;
    
    	HRESULT hr;
    	VARIANT var;
    	VariantInit(&var);
    
    	IADs * pADs = NULL;
    	IADsContainer * pADsContainer = NULL;
    	ULONG lFetch;
    
    
    	IEnumVARIANT * pEnum = NULL;
    	LPUNKNOWN pUnk = NULL;
    	BOOL bFound = FALSE;
    
    	WCHAR wszPath[200] = {0};
    	wcscpy_s(wszPath, g_pszW3SVC);
    	wcscat_s(wszPath, L"/");
    	wcscat_s(wszPath, bstrChild);
    
    	hr = ADsGetObject(wszPath, IID_IADs, (void **)&pADs);
    	if (FAILED(hr))
    		return;
    
    	hr = pADs->Get(L"ServerComment", &var);
    	if (FAILED(hr))
    	{
    		goto FUNC_RTN;
    	}
    	bstrWebSiteName = SysAllocString(var.bstrVal);
    	VariantClear(&var);
    
    	if (!bstrWebSiteName)
    	{
    		goto FUNC_RTN;
    	}
    
    	hr = pADs->QueryInterface(IID_IADsContainer, (void **)&pADsContainer);
    	SAFE_RELEASE(pADs);
    	if (FAILED(hr))
    	{
    		goto FUNC_RTN;
    	}
    
    	hr = pADsContainer->get__NewEnum(&pUnk);
    	SAFE_RELEASE(pADsContainer);
    	if (FAILED(hr))
    	{
    		goto FUNC_RTN;
    	}
    
    	hr = pUnk->QueryInterface(IID_IEnumVARIANT, (VOID **)&pEnum);
    	SAFE_RELEASE(pUnk);
    	if (FAILED(hr))
    	{
    		goto FUNC_RTN;
    	}
    
    	hr = pEnum->Next(1, &var, &lFetch);
    	while (SUCCEEDED(hr) && lFetch > 0 && !bFound)
    	{
    		IDispatch *pDisp = NULL;
    		IADs *pADsTmp =  NULL;
    		pDisp = V_DISPATCH(&var);
    		hr = pDisp->QueryInterface(IID_IADs, (void **)&pADsTmp);
    
    		if (SUCCEEDED(hr))
    		{
    			BSTR bstrName = NULL;
    			BSTR bstrClass = NULL;
    			pADsTmp->get_Name(&bstrName);
    			pADsTmp->get_Class(&bstrClass);
    			SAFE_RELEASE(pADsTmp);
    
    			if (_wcsicmp(bstrName, L"ROOT") == 0 && _wcsicmp(bstrClass, L"IIsWebVirtualDir") == 0)
    			{
    				bFound = TRUE;
    
    				WCHAR szTmp[200] = {0};
    				wsprintf(szTmp, L"%s/%s", wszPath, bstrName);
    				BSTR path = GetWebsitePath(szTmp);
    				if (path)
    				{
    					wpm.insert( CWebPathMap::value_type(bstrWebSiteName, path) );
    					bstrWebSiteName = NULL;
    				}
    				else
    				{
    				}
    			}
    			SysFreeString(bstrClass);
    			SysFreeString(bstrName);	
    		}
    
    		VariantClear(&var);
    		hr = pEnum->Next(1, &var, &lFetch);
    	}
    
    FUNC_RTN:
    	SysFreeString(bstrWebSiteName);
    	VariantClear(&var);
    	SAFE_RELEASE(pADs);
    	SAFE_RELEASE(pADsContainer);
    	SAFE_RELEASE(pUnk);
    	SAFE_RELEASE(pEnum);
    }
    
    void GetWebPathMap(CWebPathMap& wpm)
    {
    	HRESULT hr;
    	IADsContainer * pADsContainer = NULL;
    	hr = ADsGetObject(g_pszW3SVC, IID_IADsContainer, (VOID **)&pADsContainer);
    	if (FAILED(hr))
    		return;
    
    	IEnumVARIANT * pEnum = NULL;
    	LPUNKNOWN pUnk = NULL;
    
    	hr = pADsContainer->get__NewEnum(&pUnk);
    	SAFE_RELEASE(pADsContainer);
    	if (FAILED(hr))
    		return;
    
    	hr = pUnk->QueryInterface(IID_IEnumVARIANT, (VOID **)&pEnum);
    	SAFE_RELEASE(pUnk);
    	if (FAILED(hr))
    		return;
    
    	ULONG lFetch;
    	VARIANT var;
    	VariantInit(&var);
    	hr = pEnum->Next(1, &var, &lFetch);
    	while (SUCCEEDED(hr) && lFetch > 0)
    	{
    		IDispatch *pDisp = NULL;
    		IADs *pADs =  NULL;
    		pDisp = V_DISPATCH(&var);
    		hr = pDisp->QueryInterface(IID_IADs, (void **)&pADs);
    
    		if (SUCCEEDED(hr))
    		{
    			BSTR bstrName = NULL;
    			BSTR bstrClass = NULL;
    			pADs->get_Name(&bstrName);
    			pADs->get_Class(&bstrClass);
    			SAFE_RELEASE(pADs);
    
    			if (_wcsicmp(bstrClass, L"IIsWebServer") == 0)
    			{
    				AddToMap(bstrName, wpm);
    			}
    
    			SysFreeString(bstrClass);
    			SysFreeString(bstrName);
    		}
    
    		VariantClear(&var);
    		hr = pEnum->Next(1, &var, &lFetch);
    	}
    
    	SAFE_RELEASE(pEnum);
    }
    
    
    void main()
    {
    //#if defined(_UNICODE) || defined(UNICODE)
    	const char * c = setlocale(LC_ALL, "chs");
    //#endif
    	CoInitialize(NULL);
    
    	while (true)
    	{
    		CWebPathMap wpm;
    
    		GetWebPathMap(wpm);
    
    		WPM_ITERATOR this_it = wpm.begin();
    		while (this_it != wpm.end())
    		{
    			WPM_ITERATOR last_it = g_lastWPM.begin();
    			while (last_it != g_lastWPM.end())
    			{
    				if (_wcsicmp(this_it->first, last_it->first) == 0)
    				{
    					if (_wcsicmp(this_it->second, last_it->second) != 0)
    					{
    						WCHAR wszBuf[200] = {0};
    						wsprintf(wszBuf, L"网站%s的物理路径从\"%s\"变为\"%s\"\n", this_it->first, last_it->second, this_it->second);
    						wprintf(wszBuf);
    					}
    					break;
    				}
    				last_it++;
    			}
    
    			this_it++;
    		}
    
    		wpm.MoveData(g_lastWPM);
    
    		Sleep(2000);
    	}
    
    	CoUninitialize();
    }

    注:代码的功能是检测iis网站对应的物理路径是否发生改变(机器上需要安装iis)

    程序的执行过程中发现内存释放不掉,但找不出来原因,求各位技术大牛给看一下! 有微软的熟悉ADSI的技术大牛可以帮忙看一下么,非常感谢!

    2014年1月21日 5:31

答案

全部回复