发布构建问题;将std :: string对象传递给静态库函数

时间:2011-07-09 18:45:04

标签: c++ string visual-studio-2005 release-builds

这是我第一次尝试使用Visual C ++ 2005发布版本,看起来肯定存在一些差异。

我当前的错误是:

Unhandled exception at 0x6ef7d628 (msvcr80d.dll) in <program_name>: 
0xC0000005: Access violation reading location 0x6c2e6769.

我查看调用堆栈,结果发现传递给我所做的静态库函数的字符串给出了“Bad Ptr”,我不明白为什么。在调试版本中运行得很好......

这是有问题的一行:

int main()
{
    static Script luaScript("config.lua");

脚本只是我处理lua脚本文件的一个类。它是静态的,因为我希望它是一个单例,所以任何代码都可以访问它。

脚本构造函数:

Script::Script(const string &filename)
{
    luaState = lua_open();
    scriptFilename = filename; // unhandled exception occurs here; Intellisense     
                               // identifies filename as a <Bad Ptr>
                               // works perfectly fine in debug

}

我在想这可能是因为库也处于调试模式,但是当我尝试使用Release时,我似乎无法编译。

fatal error C1010: unexpected end of file while looking for precompiled header. 
Did you forget to add '#include "stdafx.h"' to your source?

我对这个文件很熟悉,但为什么我的静态库的Release版本需要它呢?没有在Debug中要求它。

好的,去获取stdafx.h文件,然后......出现了新的错误!:

fatal error C1083: Cannot open precompiled header file: 
'Release\Script.pch': No such file or directory

嗯,除了“Visual C ++ 2005希望我为Release版本做什么之外,还有什么东西可以找到这个问题的中心问题。”。

我希望有人可以提供帮助。谢谢。

2 个答案:

答案 0 :(得分:1)

修复Cannot open precompiled header file"的第一种方法是全部清理/重建。
之后,我将从发布和调试版本之间的差异开始。打开项目文件或比较visual studio中的项目设置 在担心Bad Ptr问题之前,让库在版本中构建。一旦你越过那个,Bad Ptr问题就很可能会消失 否则,我看到的唯一另一件事是你传入的是char []而不是std :: string。我认为这不是一个问题,但我会尝试

string filename = "config.lua";
static Script luaScript(filename);

在尝试了我提到的其他所有内容之后。

答案 1 :(得分:-1)

关于单例和静态初始化的排序:

The Static Initialization Fiasco

  

悲剧在于你有50%-50%的死亡几率

如何解决?

这个问题有很多解决方案,但是一个非常简单且完全可移植的解决方案是用全局函数x()替换全局Fred对象x,它通过引用返回Fred对象。

// File x.cpp

#include "Fred.h"

Fred& x()
{
    static Fred ans;
    return ans;
}
相关问题