-2

i got a very strange problem with a variable that has name 'val'. I will try my best to explain it. I cannot make any small code example that do the same issue and my project is very big so can't upload it here also.

The code i try is (part of it)

//the .cpp file
int val = 136;
int val2 = 386;
float valF = 412.f;

void GUI::MyClass::OnRender()
{
    MessageBoxW(
        0,
        std::to_wstring(val).c_str(),
        L"Error",
        MB_OK
    );
}
//in .h file there is only the GUI::MyClass class

the 3 variables can accesed from the source file, are not in any class or in any header.

So for example i do these steps;

  1. change 'val' to 3456
  2. built and run with windows debugger
  3. program show '3456' that is correct as expected...
  4. close exe
  5. change 'val' to 345
  6. built and run with windows debugger again
  7. now program show again '3456' that is wrong!

Sometimes it show the correct value and other times the previous value after each compiling.

But this is the less strange part as if i click the button 'Rebuilt Solution' and not 'Built Solution' it will always show the correct value, but i don't use precompiled headers.

Also 1 more weird part is if do the same steps with 'val2' or 'valF' and print its values always will show the correct value, even with only simple 'Build Solution'!!!

I'm not sure if i do something wrong or if visual studio has a bug on compiler, so i'm very confused now. Maybe i cannot use the variable with name 'val' in visual studio in some cases? Also i did a restart of visual studio but nothing happend.

I am very confused about this problem but i think it is a visual studio compiler bug...

Here are some details about project/visual studio/c++;

  1. Visual Studio Community 2022 (64-bit) Version 17.0.4
  2. Not Using Precompiled Headers
  3. platform toolset: Visual Studio 2022 (v143)
  4. c++ languange standard: ISO C++17 Standard (/std:c++17)
  5. SubSystem: Windows (/SUBSYSTEM:WINDOWS)
mod Zz
  • 49
  • 6
  • 1
    This question's shown code fails to meet Stackoverflow's requirements for showing a [mre]. Because of that it's unlikely that anyone here can conclusively answer the question; but only guess at the most. You need to [edit] your question to show a minimal example, no more than one or two pages of code (the "minimal" part), that everyone else can cut/paste ***exactly as shown***, compile, run, and reproduce the described issue (the "reproducible" part, this includes any ancillary information, like any input to the program). See [ask] for more information. – Sam Varshavchik Jan 07 '22 at 00:21
  • 1
    Are you *saving* the source file after modifying the variable's initial value? It sounds like sometimes your EXE is being built with an outdated source/obj file. – Remy Lebeau Jan 07 '22 at 00:25
  • @RemyLebeau the Visual Studio saves files (related to the project) on `Build` – Vlad Feinstein Jan 07 '22 at 00:37

1 Answers1

3

As Sam suggested, we can only guess here.

But I think I have a good guess, so I'm posting it as an answer.

I can reproduce the defect that you mentioned with the following steps:

  • create a header file, but do NOT add it to a project
  • define your int val = 123; in that header
  • include that header into some .cpp file

The bottom line is: the Visual Studio can't correctly determine dependencies for your project, and doesn't know that the "affected" file needs to be rebuilt. Obviously, Rebuild All option can fix that.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
  • i tried it but it still does the same – mod Zz Jan 07 '22 at 10:31
  • it looks like visual studio fails to see the changed value only in that specific variable, i think something is wrong with the variable name, I'm not use how it check for changed code but i assume it create a hash for the file and compare the previous with current hash. Then is this case it looks like it is hashing algorithm 'problem' – mod Zz Jan 07 '22 at 11:01