简体   繁体   English

发布C ++宏定义

[英]RELEASE C++ Macro Definition

My company's main application uses OLE documents. 我公司的主要应用程序使用OLE文档。 Periodically, and unpredictably, the program closes its template documents improperly. 该程序会定期且不可预测地关闭其模板文档。 So that at seemingly random times when they're opened, the OS throws STG_E_SHAREVIOLATION 因此,在打开它们的时间看似随机的情况下,操作系统会抛出STG_E_SHAREVIOLATION

I thought the problem might be the way we're closing the files when the user either exits the application or chooses File / Close from the menu. 我认为问题可能出在用户退出应用程序或从菜单中选择“文件/关闭”时我们关闭文件的方式。 After a lot of debugging / tracing, it comes down to 经过大量的调试/跟踪,可以归结为

/////////////////////////////////////////////////////////////////////////////
// '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;
}

in OLEDOC1.CPP (part of the MFC libraries). 在OLEDOC1.CPP中(MFC库的一部分)。 Specifically the RELEASE(m_lpRootStg) macro line. 特别是RELEASE(m_lpRootStg)宏行。 Prior to executing this line, trying to move or delete the document results in the OS saying that the file is in use. 在执行此行之前,尝试移动或删除文档会导致OS提示该文件正在使用中。 After this line, the file is closed and able to be moved. 在此行之后,文件已关闭并且可以移动。

I'd like to subclass this method to experiment with alternative ways of closing the file. 我想将此方法子类化,以尝试其他方法来关闭文件。 But, I cannot find the definition of the RELEASE macro anywhere. 但是,我在任何地方都找不到RELEASE宏的定义。 The closest I came was some code from IBM. 我最接近的是IBM的一些代码。 Where is this macro defined? 该宏在哪里定义? What is the definition? 什么是定义?

It's in oleimpl2.h in the MFC src directory... 它位于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM