Module identification ends with exception code c0000005

I am trying to create unit tests in Visual Studios 2012 using the Native Unit Test Project.

This is the test I have:

TEST_METHOD(CalculationsRoundTests) { int result = Calculations::Round(1.0); Assert::AreEqual(1, result); } 

Export class:

 #ifdef EXPORT_TEST_FUNCTIONS #define MY_CALCULATIONS_EXPORT __declspec(dllexport) #else #define MY_CALCULATIONS_EXPORT #endif ... class CALCULATIONS_EXPORT Calculations { ... public: static int Round(const double& x); 

Function itself:

 int Calculations::Round(const double& x) { int temp; if (floor(x) + 0.5 > x) temp = floor(x); else temp = ceil(x); return int(temp); } 

However, the test almost always fails with error code c0000005 (Access Violation). The test will fail the first time x is used or any other variable that can be declared in a function.

I followed the instructions in Unresolved externals when compiling unit tests for Visual C ++ 2012

+4
source share
2 answers

I never understood why the test will lead to access violation when it starts; however, I am sure that I did something wrong.

To remove this error, I changed the structure of my Visual Studio solution so that most of the code was in a static library project (.lib) that would contain the implementation of my program. Thus, all classes and functions for my program in the project are automatically exported, so I do not need to use __declspec (dllexport).

Then I created a small Win32 console application that will create a .exe file for my program that references the .lib project. The goal of this project is to create an executable file for my program, so everything he needed was basic, which would cause the beginning of my code in the .lib project.

After that, I was able to easily start the Native Unit Test project by simply asking him to also link to the .lib project, and since then I have had no access errors.

0
source

This is a known bug . Unfortunately, Microsoft considers this โ€œnot correctingโ€.

In short, there are two workarounds:

  • Compile the actual project in release mode and the test project in debug mode.
  • Outsourcing all the functions being checked for a static library project.
0
source

Source: https://habr.com/ru/post/1496374/


All Articles