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

本文主要是介绍一个单例模式中使用std::unique_ptr引起的莫名其妙的COFF损坏的问题(未解决),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用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 符号表
仅使用类名生成一个对象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;}
//...
};编译成功
使用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 onceextern "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:MinorLast Reviewed:1/17/2006
Keywords:kbCompiler kbAppDev kbprb KB822329 kbAudDeveloper

©2004 Microsoft Corporation. All rights reserved.

这篇关于一个单例模式中使用std::unique_ptr引起的莫名其妙的COFF损坏的问题(未解决)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/951098

相关文章

java图像识别工具类(ImageRecognitionUtils)使用实例详解

《java图像识别工具类(ImageRecognitionUtils)使用实例详解》:本文主要介绍如何在Java中使用OpenCV进行图像识别,包括图像加载、预处理、分类、人脸检测和特征提取等步骤... 目录前言1. 图像识别的背景与作用2. 设计目标3. 项目依赖4. 设计与实现 ImageRecogni

python管理工具之conda安装部署及使用详解

《python管理工具之conda安装部署及使用详解》这篇文章详细介绍了如何安装和使用conda来管理Python环境,它涵盖了从安装部署、镜像源配置到具体的conda使用方法,包括创建、激活、安装包... 目录pytpshheraerUhon管理工具:conda部署+使用一、安装部署1、 下载2、 安装3

Mysql虚拟列的使用场景

《Mysql虚拟列的使用场景》MySQL虚拟列是一种在查询时动态生成的特殊列,它不占用存储空间,可以提高查询效率和数据处理便利性,本文给大家介绍Mysql虚拟列的相关知识,感兴趣的朋友一起看看吧... 目录1. 介绍mysql虚拟列1.1 定义和作用1.2 虚拟列与普通列的区别2. MySQL虚拟列的类型2

使用MongoDB进行数据存储的操作流程

《使用MongoDB进行数据存储的操作流程》在现代应用开发中,数据存储是一个至关重要的部分,随着数据量的增大和复杂性的增加,传统的关系型数据库有时难以应对高并发和大数据量的处理需求,MongoDB作为... 目录什么是MongoDB?MongoDB的优势使用MongoDB进行数据存储1. 安装MongoDB

关于@MapperScan和@ComponentScan的使用问题

《关于@MapperScan和@ComponentScan的使用问题》文章介绍了在使用`@MapperScan`和`@ComponentScan`时可能会遇到的包扫描冲突问题,并提供了解决方法,同时,... 目录@MapperScan和@ComponentScan的使用问题报错如下原因解决办法课外拓展总结@

mysql数据库分区的使用

《mysql数据库分区的使用》MySQL分区技术通过将大表分割成多个较小片段,提高查询性能、管理效率和数据存储效率,本文就来介绍一下mysql数据库分区的使用,感兴趣的可以了解一下... 目录【一】分区的基本概念【1】物理存储与逻辑分割【2】查询性能提升【3】数据管理与维护【4】扩展性与并行处理【二】分区的

使用Python实现在Word中添加或删除超链接

《使用Python实现在Word中添加或删除超链接》在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能,本文将为大家介绍一下Python如何实现在Word中添加或... 在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能。通过添加超

MybatisGenerator文件生成不出对应文件的问题

《MybatisGenerator文件生成不出对应文件的问题》本文介绍了使用MybatisGenerator生成文件时遇到的问题及解决方法,主要步骤包括检查目标表是否存在、是否能连接到数据库、配置生成... 目录MyBATisGenerator 文件生成不出对应文件先在项目结构里引入“targetProje

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,