dascandy / hippomocks

GNU Lesser General Public License v2.1
196 stars 67 forks source link

[cpp11 branch] memory access violation with multiple mock repositories #94

Open torbjoernk opened 6 years ago

torbjoernk commented 6 years ago

When using more than one mock repository, the destructor of the second mock repository fails with a memory access violation at hippomocks.h:942 when calling TestFinished() on the reporter of the second repository as MockRepoInstanceHolder<0>::instance = nullptr; and MockRepoInstanceHolder<0>::reporter = nullptr; has been called in the destructor of the first mock repository.

Sample code:

#include <boost/test/unit_test.hpp>
#include <hippomocks.h>

struct Foo
{
  virtual ~Foo() noexcept = 0;
  virtual int foo() = 0;
};

struct Bar
{
  virtual ~Bar() noexcept = 0;
  virtual int bar() = 0;
};

struct Fixture
{
  Fixture()
  {
    mocks_foo.autoExpect = false;
    mocks_bar.autoExpect = false;

    foo.reset(mocks_foo.Mock<Foo>());
    bar.reset(mocks_bar.Mock<Bar>());

    mocks_foo.ExpectCallDestructor(foo.get());
    mocks_bar.ExpectCallDestructor(bar.get());
  }

  HippoMocks::MockRepository mocks_foo;
  HippoMocks::MockRepository mocks_bar;
  std::shared_ptr<Foo> foo;
  std::shared_ptr<Bar> bar;
};

BOOST_FIXTURE_TEST_SUITE(HippoMocksTest, Fixture)

BOOST_AUTO_TEST_CASE(hippomocks_two_repos)
{
}

BOOST_AUTO_TEST_SUITE_END()
tnovotny commented 6 years ago

Hmm, it looks like there is a singleton being used now, so using two repos no longer works. The example is a skinned down example where, due to complex setup, it was necessary to introduce a second repository to change the order of destructor calls, or rather the point in time when the expectations on a mock are evaluated. Is there any way to achieve such an effect using other means?

torbjoernk commented 6 years ago

We were able to workaround our need for two separate mock repositories. Thus, this issue isn't relevant for us any more.