0

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

user1114365
  • 35
  • 2
  • 3

1 Answers1

4

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

These symptoms make sense. "Release" mode tells the compiler to turn optimizations on, and since you never use the variables that you declare, the compiler helpfully optimizes them out altogether. There's no point in going through the motions of creating and assigning something if you'll never use it again.

That's why it's telling you that the symbol is not found, because its definition was optimized out.

On the other hand, "Debug" mode it disables optimizations. Thus, it goes through the motions of creating these variables and assigning values to them, even though you may never use them. That's the whole point of debug mode—so you can debug your application without interference from the optimizing behavior of the compiler, even when it's not completely written yet.

If you're desperate to make it work like you expect with optimizations enabled (i.e., in "Release" mode), then you can simply use the values of the variables you assign. That will prevent the compiler from optimizing them out. For example, you can simply output the strings to the debugger:

#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();
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574