c++stringvisual-studio-2005release-builds

Release build problems; Passing a std::string object to a static library function


This is the first time I've tried to use the release build with Visual C++ 2005 and it seems there's definitely some differences.

My current error is:

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

I look into the call stack and it turns out that a string passed to a function of a static library that I've made is giving a "Bad Ptr" and I have no idea why. Works just fine in the debug build...

Here's the line in question:

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

Script is just a class I made that handles lua script files. It's static because I want it to be a singleton so any code can access it.

The Script constructor:

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

}

I'm thinking it may be because the library is in debug mode too, but I can't seem to get that to compile when I try to use Release.

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

I'm kind of familiar with that file, but why do I need it for the Release build of my static library? Didn't ask for it in the Debug.

Ok, go get the stdafx.h file and... a new error has arrived!:

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

Well, it's kind of hard to find a central question for all of this other than, "What in the world does Visual C++ 2005 want me to do for Release builds!?".

I hope someone can be of assistance. Thank You.


Solution

  • The #1 way to fix Cannot open precompiled header file" is to do a clean/rebuild all.
    After that, I would start with the differences between the release and debug builds. Either open the project file or compare the project settings from within visual studio.
    Get the library to build in release before worrying about the Bad Ptr issue. It's very likely that the Bad Ptr problem will go away once you get past that.
    Otherwise, the only other thing I see is that you're passing in a char[] and not a std::string. I don't think this is really a problem, but I'd try

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

    after trying everything else I've mentioned.