title: Access violation reading location using GTest without any complicated testing
body: <p>I got stucked for a week on a problem that I cannot solve by my own.</p>
  
  <p>DESCRIPTION:
  My boss decided to implement C++ objects in C language, because he likes GTesting method (that cant use 90% of it's possibilities, because C doesn't containt objects).
  I did it like wise people did in this website : <a href="http://research.engineering.wustl.edu/~beardj/Mixed_C_C++.html" rel="nofollow">http://research.engineering.wustl.edu/~beardj/Mixed_C_C++.html</a></p>
  
  <p>I used them in bigger function, where it works great, without any problems, no memory leaks, nothing... Fake functions are called via interface perfectly.</p>
  
  <p>Problem cames when I want to apply GTest on this function, that calls these C++ objects via interface (dual purpose header), or even when I want to call GTest at all.</p>
  
  <p>Here are source codes:</p>
  
  <p>This is file including all GTest and GMock methods. It is common file that works elsewhere.</p>
  
  <p><strong>InitGMock.h</strong></p>
  
  <pre><code>#ifndef INITGMOCK_H_
  #define INITGMOCK_H_
  
  
  #include "GTest/gtest.h"
  #include "GMock/gmock.h"
  
  
  #define UNREFERENCED_PARAMETER(P)      (void)(P)
  
  using ::testing::Return;
  using ::testing::Cardinality;
  using ::testing::AtLeast;
  using ::testing::_;
  using ::testing::An;
  using ::testing::A;
  using ::testing::Ge;
  using ::testing::Eq;
  using ::testing::InSequence;
  using ::testing::internal::ExpectationTester;
  
  ... and so on, using all testing methods
  </code></pre>
  
  <p>This is main cpp file.</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include "InitGMock.h"  
  extern "C"
  {
  #include "LPAll_Testing_3.h" //all includes from C files
  }
  
  
  int main(int argc, char **argv)
  {
  testing::InitGoogleTest(&amp;argc, argv);
  
  ::testing::TestEventListeners&amp; listeners = ::testing::UnitTest::GetInstance()-&gt;listeners();
  listeners.Append(new MemoryLeakListener);
  
  
  RUN_ALL_TESTS();
  
  getchar(); // keep console window open until Return keystroke 
  return 0;
  }
  </code></pre>
  
  <p>and here are my tests</p>
  
  <p><strong>Tests.cpp</strong></p>
  
  <pre><code>#include "LPAll_Testing_3.h"
  #include "InitGMock.h"
  
  TEST(oLocalPsiTestingClass, FirstTest) 
  {
  
      EXPECT_EQ(7,7);
  }  
  </code></pre>
  
  <p>What I have figured out by now:
  1. From time to time program doesn't crash, test is made (but fails).</p>
  
  <ol>
  <li><p>When test fails, it fails in <code>dbghook.c</code> file</p></li>
  <li><p>TEST() is called twice, for reason I dont know. First immediately after program start, second after <code>RUN_ALL_TESTS();</code> command</p></li>
  <li><p>Error after when program ends:
  First-chance exception at 0x0047aae0 in LP_Testing.exe: 0xC0000005: Access violation reading location 0xcccccc1c.
  unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.</p></li>
  <li><p>I tried to use other main signatures:</p>
  
  <ul>
  <li>int main(int argc, char **argv)  //ORIGINAL</li>
  <li>//int main(int argc, _TCHAR* argv[])</li>
  <li>//int _tmain(int argc, _TCHAR* argv[])</li>
  </ul></li>
  </ol>
  
  <p>but no one is working correctly</p>
  
  <p>If someone has idea how to solve this, I would be incredibly happy, because I am starting to be desperate. :-)</p>
  
  <p>Thanks, have a nice day. If you will need some other informations or source codes, ask. :-)</p>
  
  <p>EDIT:
  I found another strange issue.
  When I call macro <code>EXPECT_EQ(7,7)</code> it throws error <code>There is no source code available for the current location.</code> - VS2005 error message. I have included gtest.lib and gmock.lib (and and directory where they are), so it should see this command. Also when I want to see declaration &amp; definition of this it will redirect me to gtest.h</p>