开发者

specific line of code exiting with code 255 after changing code generation

Well I tried to compile a small testing app I am working on.

So to keep things short and simple:

When I set my code generation from "Multi Threaded DLL" to "Multi Threaded" to get rid of some dependencies, the following line of code crashes my application (where it usually runs without any flaws)

The crash happens when I want to convert a short path to a long path. as such:

LPCSTR tmp = reinterpret_cast<LPCSTR>(getenv("Temp"));
GetLongPathNameA(tmp,tempFolder,MAX_PATH);

The crash specifically occurs at the first line:

LPCSTR tmp = reinterpret_cast<LPCSTR>(getenv("Temp"));

So any ideas here开发者_Python百科 why it suddenly stops working when you switch the code generation mode? Thanks!

EDIT:

After some code-rewriting I managed to find out it specifically crashes when executing

getenv("Temp");

very very weird seeing as it does work in the other mode


Be sure that all projects, (and all files of those projects) are consistently set to compile and link against the same version of the runtime libraries, i.e. multithreaded static, in your case. If you mix these options the compiles and linked program will have undefined behavior. Also assure that you compile and link against the correct versions of the external libraries (MFC, etc..). In some cases you are restricted to use certain versions of runtime, e.g. if you interoperate with .Net, you have to use the multitheaded dll version.


You should check the return value of getenv() before accessing it:

LPCSTR tmp = getenv("Temp");
if(tmp != NULL)
    // do something with tmp

My guess is that your program can't read the environment variable and accessing the resulting NULL-pointer will lead to program crashes.

Microsoft recommends the usage of getenv_s() instead, here's their MSDN sample slightly modified for your job:

char *tmp;
size_t requiredSize;

getenv_s(&requiredSize, NULL, 0, "Temp");
tmp = (char *) malloc(requiredSize * sizeof(char));
if (tmp != NULL)
{
    getenv_s(&requiredSize, tmp, requiredSize, "Temp");
    if(tmp != NULL)
        // do something with tmp

    free(tmp);
}

I would personally recommend switching to the WinAPI function GetEnvironmentVariable() instead, this will give you a more detailed error message (use GetLastError() if the function failed) which might help you get to the root of your problem (or fix it by using one of these alternatives).


Try to rebuild the project. Clean it, make sure nothing is left in output folder, for good measure delete .ncb as well, then build.


Have you specified libcmt.lib and libcpmt.lib as dependencies in linker settings after switching from dynamic runtime to static? If not, please try that. And then make a rebuild.


You should not use reinterpret_cast ,because it is for objects that are inheritted by or inheritting from other classes. Just use static_cast for basic types or pointers to basic types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜