-1

I was coding in win32, and my program actually works in debug mode in vs, however not in release mode and not outside vs.

int _tmain(int argc, _TCHAR* argv[])
{
    //assert that there are 3 parameters.
    assert(argc==4);
    LPCTSTR inputPath = argv[1];
    LPCTSTR sharedName = argv[2];
    LPCTSTR logPath = argv[3];

sometimes argc is not correct (over 300000, while it should be 4), and sometimes the

LPCTSTR sharedName = argv[2]; 

line is just ignored! when debugging this program in release mode, it jumps over it, and when hoovering above the variable name nothing happens. When right-clicking a variable and choosing Add Watch, I get the error logPath CXX0017: Error: symbol "logPath" not found
of course, I have set the command arguments in vs to be "a b c" (without the quotes)

What could it be? running the simplified program: // test.cpp : Defines the entry point for the console application. //

#include "stdafx.h"
#include "stdafx.h"
#include <windows.h>
#include <assert.h>
#include "conio.h"

int _tmain(int argc, _TCHAR* argv[])
{
    assert(argc==4);
    LPCTSTR inputPath = argv[1];
    LPCTSTR sharedName = argv[2];
    LPCTSTR logPath = argv[3];
    _getch();
}

yields the same result. the debugger just jumps to the getch line, and if I try to add watch, I get logPath CXX0017: Error: symbol "logPath" not found
inputPath CXX0017: Error: symbol "inputPath" not found
sharedName CXX0017: Error: symbol "sharedName" not found

4

1 回答 1

4

在发布模式下调试这个程序时,它会跳过它,当悬停在变量名上方时,什么也没有发生。右键单击变量并选择 Add Watch 时,出现错误 logPath CXX0017: Error: symbol "logPath" not found

这些症状是有道理的。“发布”模式告诉编译器打开优化,并且由于您从不使用您声明的变量,编译器有助于将它们完全优化出来。如果您永远不会再使用它,那么创建和分配某些东西的动作是没有意义的。

这就是为什么它告诉您找不到该符号的原因,因为它的定义已被优化。

另一方面,“调试”模式会禁用优化。因此,它经历了创建这些变量并为它们分配值的动作,即使您可能永远不会使用它们。这就是调试模式的全部意义——因此您可以在不受编译器优化行为干扰的情况下调试您的应用程序,即使它还没有完全编写好。

如果您迫切希望通过启用优化(即在“发布”模式下)使其像您期望的那样工作,那么您可以简单地使用您分配的变量的值。这将阻止编译器优化它们。例如,您可以简单地将字符串输出到调试器:

#include "stdafx.h"
#include <windows.h>
#include <assert.h>
#include <conio.h>

int _tmain(int argc, _TCHAR* argv[])
{
   assert(argc==4);
   LPCTSTR inputPath  = argv[1];
   LPCTSTR sharedName = argv[2];
   LPCTSTR logPath    = argv[3];

   OutputDebugString(inputPath);
   OutputDebugString(sharedName);
   OutputDebugString(logPath);

   _getch();
}
于 2012-05-11T19:20:53.887 回答