RELEASE C ++宏定义

时间:2017-04-05 15:17:36

标签: c++ macros mfc release ole

我公司的主要应用程序使用OLE文档。程序定期和不可预测地不正确地关闭其模板文档。因此,在看似随机的时候,操作系统会抛出STG_E_SHAREVIOLATION

我认为问题可能是当用户退出应用程序或从菜单中选择文件/关闭时我们关闭文件的方式。经过大量的调试/跟踪后,它归结为

/////////////////////////////////////////////////////////////////////////////
// 'Compound File' enabling in COleDocument

BOOL COleDocument::OnNewDocument()
{
    // call base class, which destroys all items
    if (!CDocument::OnNewDocument())
        return FALSE;

    // for file-based compound files, need to create temporary file
    if (m_bCompoundFile && !m_bEmbedded)
    {
        // abort changes to the current docfile
        RELEASE(m_lpRootStg);

        // create new temporary docfile
        LPSTORAGE lpStorage;
        SCODE sc = ::StgCreateDocfile(NULL, STGM_DELETEONRELEASE|
            STGM_READWRITE|STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_CREATE,
            0, &lpStorage);
        if (sc != S_OK)
            return FALSE;

        ASSERT(lpStorage != NULL);
        m_lpRootStg = lpStorage;
    }

    return TRUE;
}

在OLEDOC1.CPP(MFC库的一部分)中。特别是RELEASE(m_lpRootStg)宏行。在执行此行之前,尝试移动或删除文档会导致操作系统说该文件正在使用中。在此行之后,文件将关闭并可以移动。

我想将此方法子类化,以尝试关闭文件的替代方法。但是,我无法在任何地方找到RELEASE宏的定义。我最接近的是来自IBM的一些代码。这个宏定义在哪里?定义是什么?

1 个答案:

答案 0 :(得分:1)

它位于MFC src目录中的oleimpl2.h ...

#ifndef _DEBUG
// generate smaller code in release build
#define RELEASE(lpUnk) _AfxRelease((LPUNKNOWN*)&lpUnk)
#else
// generate larger but typesafe code in debug build
#define RELEASE(lpUnk) do \
    { if ((lpUnk) != NULL) { (lpUnk)->Release(); (lpUnk) = NULL; } } while (0)
#endif