From 1627fc369d62ee758952d0eca99fc42fb958f04d Mon Sep 17 00:00:00 2001 From: Roman Golovanov Date: Thu, 18 May 2017 20:42:36 +0300 Subject: [PATCH] Don't throw nested exception from dtor of Mock object There is a problem happened when GMock is used with 3rdParty UT-engine based on exceptions handling. If I enables throwing exceptions from GMock: ::testing::GTEST_FLAG(throw_on_failure) = true Then in test case if I created a local Mock object with invalid expectation call then I will get exception in exception: Because of invalid arguments passed to the method. During stack unwinding we will destroy Mock object and will call VerifyAndClearExpectationsLocked(); from~FunctionMockerBase() These behavior obviously leads to process termination. The suggestion is to check whether we already in unhandled exception or not before Verifing expectations in destructor: if (!std::uncaught_exception()) { VerifyAndClearExpectationsLocked(); } --- googlemock/include/gmock/gmock-spec-builders.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/googlemock/include/gmock/gmock-spec-builders.h b/googlemock/include/gmock/gmock-spec-builders.h index fed7de6..233ba77 100644 --- a/googlemock/include/gmock/gmock-spec-builders.h +++ b/googlemock/include/gmock/gmock-spec-builders.h @@ -1467,7 +1467,8 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { virtual ~FunctionMockerBase() GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { MutexLock l(&g_gmock_mutex); - VerifyAndClearExpectationsLocked(); + if (!std::uncaught_exception()) + VerifyAndClearExpectationsLocked(); Mock::UnregisterLocked(this); ClearDefaultActionsLocked(); }