I build a test project (CxxTest) with Visual Studio and immediately execute it via post-build event. This works and did so for a long time already. Now I have different behaviour in the execution in post-build event compared to manually execute from command line shell. The following code behaves different in post build event compared to starting the exe manually or with Visual Studio debugger:
// getValueFromStruct returns empty string when executed
// as post build event, but returns correct value when executed manually
std::string expectedValue1("Val_1");
TS_ASSERT_EQUALS(expectedValue1, getValueFromStruct(res1));
This is the helper function called:
std::string getValueFromStruct(tResult pResult)
{
std::string result = "";
if (pResult.pCharDetails != NULL)
{
for (int i=0; i < pResult.iNumResults; i++)
{
result.append(1, static_cast<char>(pResult.pCharDetails[i].wChar1));
}
}
// also the cout of the result is empty when executed from
// build event, but is filled with data when in shell
std::cout << ++callCount << " : " << result<< std::endl;
return result;
}
It seems the issue arises only when getValueFromStruct(tResult) is called for more than one instance of tResult in the same method.
Are there differences between environment, runtime library or something else between the two ways? Is Visual Studio manipulating the command line environment for a build event?
The issue arises in Release and x64 mode only. And it arises on my machine only, not on the build server. But it also arises when built on my machine via build script like the server does.
I found 3 ways to by-pass the problem:
- by using a temporary variable rather than pass the result of a function directly as argument.
- Disable Optimization for the test.exe
- Set "Inline Function Expansion" to "Disabled /Ob0" for the test.exe.
But it does not explain why it is behaving differently in build event and regular command line.