一个单例模式中使用std::unique_ptr引起的莫名其妙的COFF损坏的问题(未解决)

cpp 复制代码
使用static std::unique_ptr和static std::shared_ptr都不行


struct     IElementAgendaEvents
{
    //! Called to allow listeners to modify the agenda by adding/removing entries before applying tool operation. Return true if entries added or invalidated.
    virtual bool _DoModifyAgendaEntries (ElementAgendaP agenda, AgendaOperation, AgendaModify) {return false;}

    //! Called to allow listeners to copy additional information from source to destination not appropriate to tool operation.
    virtual void _OnPreCopyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, ElementCopyContextP) {};

    //! Called before the tool operation is applied to the agenda.
    virtual void _OnPreModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};

    //! Called after the tool operation is applied to the agenda.
    virtual void _OnPostModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};

//! @cond DONTINCLUDEINDOC
    //! Called to allow custom clipboard formats to be added for the elements in the agenda.
    virtual void _DoAddDeferredClipboardFormats (ElementAgendaP, AgendaOperation, AgendaModify, GuiDataObject*) {}

    //! Called to allow listener to participate in element set dynamics. See RedrawGroupInfo for return status meaning.
    virtual bool _OnRedrawGroupEvent (ElementAgendaCP, AgendaOperation, AgendaModify, RedrawGroupInfo const*) {return false;}

    virtual bool Dummy1 (void*) {return false;}
//! @endcond
};


struct ElementAgendaEvents : public DgnPlatform::IElementAgendaEvents
{

static ElementAgendaEvents&  GetInstance()
{
    static std::unique_ptr<ElementAgendaEvents> _Instance = nullptr; 
    if (nullptr == _Instance)
         _Instance.reset(new ElementAgendaEvents());
    return *_Instance;
}
//...
};


编译错误:
ElementAgendaEvents.obj : fatal error LNK1235: 损坏或无效的 COFF 符号表
cpp 复制代码
仅使用类名生成一个对象


struct     IElementAgendaEvents
{
    //! Called to allow listeners to modify the agenda by adding/removing entries before applying tool operation. Return true if entries added or invalidated.
    virtual bool _DoModifyAgendaEntries (ElementAgendaP agenda, AgendaOperation, AgendaModify) {return false;}

    //! Called to allow listeners to copy additional information from source to destination not appropriate to tool operation.
    virtual void _OnPreCopyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, ElementCopyContextP) {};

    //! Called before the tool operation is applied to the agenda.
    virtual void _OnPreModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};

    //! Called after the tool operation is applied to the agenda.
    virtual void _OnPostModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};

//! @cond DONTINCLUDEINDOC
    //! Called to allow custom clipboard formats to be added for the elements in the agenda.
    virtual void _DoAddDeferredClipboardFormats (ElementAgendaP, AgendaOperation, AgendaModify, GuiDataObject*) {}

    //! Called to allow listener to participate in element set dynamics. See RedrawGroupInfo for return status meaning.
    virtual bool _OnRedrawGroupEvent (ElementAgendaCP, AgendaOperation, AgendaModify, RedrawGroupInfo const*) {return false;}

    virtual bool Dummy1 (void*) {return false;}
//! @endcond
};


struct ElementAgendaEvents : public DgnPlatform::IElementAgendaEvents
{

static ElementAgendaEvents&  GetInstance()
{
    static ElementAgendaEvents obj;
    return obj;

}
//...
};

编译成功
cpp 复制代码
使用std::unique_ptr和std::shared_ptr是可以的。
因为如果使用了static,说明这个变量是要被"暴露"在外面的,它虽然在函数内部,但它的名字是和其他函数一样,暴露在外面。


struct     IElementAgendaEvents
{
    //! Called to allow listeners to modify the agenda by adding/removing entries before applying tool operation. Return true if entries added or invalidated.
    virtual bool _DoModifyAgendaEntries (ElementAgendaP agenda, AgendaOperation, AgendaModify) {return false;}

    //! Called to allow listeners to copy additional information from source to destination not appropriate to tool operation.
    virtual void _OnPreCopyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, ElementCopyContextP) {};

    //! Called before the tool operation is applied to the agenda.
    virtual void _OnPreModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};

    //! Called after the tool operation is applied to the agenda.
    virtual void _OnPostModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};

//! @cond DONTINCLUDEINDOC
    //! Called to allow custom clipboard formats to be added for the elements in the agenda.
    virtual void _DoAddDeferredClipboardFormats (ElementAgendaP, AgendaOperation, AgendaModify, GuiDataObject*) {}

    //! Called to allow listener to participate in element set dynamics. See RedrawGroupInfo for return status meaning.
    virtual bool _OnRedrawGroupEvent (ElementAgendaCP, AgendaOperation, AgendaModify, RedrawGroupInfo const*) {return false;}

    virtual bool Dummy1 (void*) {return false;}
//! @endcond
};


struct ElementAgendaEvents : public DgnPlatform::IElementAgendaEvents
{

static ElementAgendaEvents&  GetInstance()
{
    /*static*/ std::unique_ptr<ElementAgendaEvents> _Instance = nullptr; 
    if (nullptr == _Instance)
         _Instance.reset(new ElementAgendaEvents());
    return *_Instance;
}
//...
};


编译成功

看到网上有人说这是一个编译器的bug:

有文章解释如下:

LNK1254, LNK1284, and LNK1235 linker errors may occur while compiling a C source file with the /clr compiler option (822329)

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ## LNK1254, LNK1284, and LNK1235 linker errors may occur while compiling a C source file with the /clr compiler option (822329) *** ** * ** *** The information in this article applies to: * Microsoft Visual C++ .NET (2003) * Microsoft Visual C++ .NET (2002) *** ** * ** *** ### SYMPTOMS When you compile a C source file (unmanaged source) with other managed source files that refer to symbols that are defined in the unmanaged source, and you specify the /clr compiler switch, you receive the following linker error message in Visual C++ .NET 2002: LINK : fatal error LINK1254: metadata for symbol symbol name inconsistent with COFF symbol table You receive the following linker errors in Visual C++ .NET 2003: <managed object file>: fatal error LNK1284: metadata inconsistent with COFF symbol table: symbol '<symbol-name>' (0A000008) mapped to '<symbol-name>' (06000001) in <unmanaged object file> <unmanaged object file>: fatal error LNK1235: corrupt or invalid COFF symbol table ### CAUSE The earlier versions of Visual C++ may not support the C or C++ source files with the /clr switch because earlier versions of Visual C++ do not have managed code or the /clr switch. ### RESOLUTION Compile the unmanaged source (including the C source files) into an object file. Similarly, compile files that constitute the managed files of the project into another object file. Link the object files that are generated from the managed and unmanaged source files into a single executable by using the linker. The following steps assume that you have unmanaged source in UnManaged.c and that you have managed source in Managed.cpp. To build your project, follow these steps: 1. Compile UnManaged.c with the following command: cl /c UnManaged.c 2. Compile Managed.cpp with the following command: cl /clr /c Managed.cpp 3. Link the object files that are generated in steps 1 and 2 with the following command: link /NODEFAULTLIB:LIBC Managed.obj UnManaged.obj ### STATUS This behavior is by design. ### MORE INFORMATION #### Steps to Reproduce the Behavior 1. Paste the following code in Notepad, and then save the file as UnManaged.h: #include <stdio.h> // Both of these generate LNK1254 in 2002 //Generates LNK1235 - In 2003 //int willNotLink(unsigned char data[20]); //int willLink(unsigned char *data); //Generates LNK1284 - In 2003 int willLink(unsigned char data[20]); int willNotLink(unsigned char *data); 2. Paste the following code in Notepad, and then save the file as UnManaged.c: //defines two functions to be referred in a C++ file. #include "UnManaged.h" int willLink(unsigned char *data) { printf("This is the function named willLink\n"); return 0; } int willNotLink(unsigned char data[20]) { printf("This is the function named willNotLink\n"); return 0; } 3. Paste the following code in Notepad, and then save the file as Managed.cpp: // This is the main file. #pragma once extern "C" { #include "UnManaged.h" } #using <mscorlib.dll> using namespace System; namespace LinkerProblem { public __gc class Class1 { public: void test() { unsigned char data[20]; willLink(data); willNotLink(data); } }; }; void main() { LinkerProblem::Class1 *myClass = new LinkerProblem::Class1(); myClass->test(); } 4. At the Visual C++ .NET command prompt, type the following command: cl /clr Managed.cpp UnManaged.c 5. Notice that you receive the linker problem that is described in the "Symptoms" section. In Visual C++ .NET 2002, you receive the LNK1254 linker error, and in Visual C++ .NET 2003 you receive the LNK1284 linker error. 6. To see the LNK1235 linker error in Visual C++ .NET 2003, modify the contents of the UnManaged.h header file. To do this, uncomment the two declarations under the following comment: //Generates LNK1235 - In 2003 and then comment the two declarations under the following comment: //Generates LNK1284 - In 2003 7. Compile the code by running the command that is specified in step 4 at the Visual Studio .NET 2003 command prompt. You receive the LNK1235 linker error in Visual C++ .NET 2003. *** ** * ** *** | Modification Type: | Minor | Last Reviewed: | 1/17/2006 | | Keywords: | kbCompiler kbAppDev kbprb KB822329 kbAudDeveloper ||||| |--------------------|-------|----------------|-----------|---|---| *** ** * ** *** | |

|---------------------------------------------------|
| ©2004 Microsoft Corporation. All rights reserved. |

相关推荐
点云SLAM8 小时前
C++ 常见面试题汇总
java·开发语言·c++·算法·面试·内存管理
UnnamedOrange11 小时前
ROS2 配置 linter 的代码格式化工具为 clang-format
c++·cmake
Dobby_0511 小时前
【面试题】C++系列(一)
c++·面经
一拳一个呆瓜11 小时前
【MFC】对话框节点属性:Language(语言)
c++·mfc
ArabySide12 小时前
【C#】 资源共享和实例管理:静态类,Lazy<T>单例模式,IOC容器Singleton我们该如何选
单例模式·c#·.net core
点云侠12 小时前
解决Visual Studio 2022编译工程速度慢的问题
开发语言·c++·ide·算法·计算机视觉·visual studio
ajassi200013 小时前
开源 C++ QT Widget 开发(十三)IPC通讯--本地套接字 (Local Socket)
linux·c++·qt·开源
Q741_14713 小时前
C++ 前缀和 高频笔试考点 实用技巧 牛客 DP34 [模板] 前缀和 题解 每日一题
开发语言·c++·算法·前缀和·牛客网
YaoYuan932314 小时前
C++ 容器——unordered_xxx
c++
凤年徐14 小时前
C++类和对象(上):从设计图到摩天大楼的构建艺术
c语言·开发语言·c++·类和对象