STDAPI DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppvOut)
{
CConMeExtClassFactory *pcf; *ppvOut = NULL; if(IsEqualIID(rclsid,CLSID_ContextMenuExt))
{
pcf = new CConMeExtClassFactory; return pcf->QueryInterface(riid,ppvOut);
} return CLASS_E_CLASSNOTAVAILABLE;
}
CContextMenuExt::CContextMenuExt()
{
m_cRef = 0L;
m_pDataObj = NULL;
g_nDllRefCount++;
}
CContextMenuExt::~CContextMenuExt()
{
if(m_pDataObj)
m_pDataObj->Release(); g_nDllRefCount--;
}
{
m_cRef = 0L;
g_nDllRefCount++;
}CConMeExtClassFactory::~CConMeExtClassFactory()
{
g_nDllRefCount--;
}
{
if(--m_cRef)
return m_cRef; delete this; return 0L;
}
pcf会被回收,但它指向的内存需要delete
但是pcf所指向的内存区域,是new出来的,new的内存是要在堆上开辟,不会自动释放。所以pcf自身(指向内存的地址)被销毁了,但pcf指向的内存地址却没有被销毁,而且指向此内存的地址也没有了,造成无法释放的内存区域。
有几个NEW,就要有几个DELETE
不然就会泄漏。
用了new就是程序员分配而不是系统分配了 所以在函数结束处并不会自己释放,必须程序员手动释放