From: Adrian Hamza <adriah@fb.com>
Date: Mon, 9 Mar 2015 21:04:47 +0000 (-0700)
Subject: Add null check to avoid crash in unit tests that use mock singletons.
X-Git-Tag: v0.31.0~17
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=82bb86218da48157b405b3eacb916bb4acaf11f3;p=folly.git

Add null check to avoid crash in unit tests that use mock singletons.

Summary: Some unit tests owned by my team are failing due to segmentation fault in SingletonHolder<T>::registerSingletonMock -> SingletonHolder<T>::destroyInstance.

Test Plan: Run folly unit tests and my unit tests.

Reviewed By: henryf@fb.com

Subscribers: trunkagent, folly-diffs@, yfeldblum

FB internal diff: D1873889

Signature: t1:1873889:1425925156:29d54092939d7e9debea3fd55f7105fd320e987e

Blame Revision: 91f4942e
---

diff --git a/folly/experimental/Singleton-inl.h b/folly/experimental/Singleton-inl.h
index 302fbc99..c853da65 100644
--- a/folly/experimental/Singleton-inl.h
+++ b/folly/experimental/Singleton-inl.h
@@ -92,16 +92,18 @@ template <typename T>
 void SingletonHolder<T>::destroyInstance() {
   state_ = SingletonHolderState::Dead;
   instance_.reset();
-  auto wait_result = destroy_baton_->timed_wait(
-    std::chrono::steady_clock::now() + kDestroyWaitTime);
-  if (!wait_result) {
-    print_destructor_stack_trace_->store(true);
-    LOG(ERROR) << "Singleton of type " << type_.name() << " has a "
-               << "living reference at destroyInstances time; beware! Raw "
-               << "pointer is " << instance_ptr_ << ". It is very likely "
-               << "that some other singleton is holding a shared_ptr to it. "
-               << "Make sure dependencies between these singletons are "
-               << "properly defined.";
+  if (destroy_baton_) {
+    auto wait_result = destroy_baton_->timed_wait(
+      std::chrono::steady_clock::now() + kDestroyWaitTime);
+    if (!wait_result) {
+      print_destructor_stack_trace_->store(true);
+      LOG(ERROR) << "Singleton of type " << type_.name() << " has a "
+                 << "living reference at destroyInstances time; beware! Raw "
+                 << "pointer is " << instance_ptr_ << ". It is very likely "
+                 << "that some other singleton is holding a shared_ptr to it. "
+                 << "Make sure dependencies between these singletons are "
+                 << "properly defined.";
+    }
   }
 }