一个单例模式中使用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

相关文章

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

如何解决线上平台抽佣高 线下门店客流少的痛点!

目前,许多传统零售店铺正遭遇客源下降的难题。尽管广告推广能带来一定的客流,但其费用昂贵。鉴于此,众多零售商纷纷选择加入像美团、饿了么和抖音这样的大型在线平台,但这些平台的高佣金率导致了利润的大幅缩水。在这样的市场环境下,商家之间的合作网络逐渐成为一种有效的解决方案,通过资源和客户基础的共享,实现共同的利益增长。 以最近在上海兴起的一个跨行业合作平台为例,该平台融合了环保消费积分系统,在短

pdfmake生成pdf的使用

实际项目中有时会有根据填写的表单数据或者其他格式的数据,将数据自动填充到pdf文件中根据固定模板生成pdf文件的需求 文章目录 利用pdfmake生成pdf文件1.下载安装pdfmake第三方包2.封装生成pdf文件的共用配置3.生成pdf文件的文件模板内容4.调用方法生成pdf 利用pdfmake生成pdf文件 1.下载安装pdfmake第三方包 npm i pdfma