Add STATIC_AVOID_DESTRUCTION for ObjectLibrary/Registry (#9464)

Summary:
This change should guarantee that the default ObjectLibrary/Registry are long-lived and not destroyed while the process is running.  This will prevent some issues of them being referenced after they were destroyed via the static destruction.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/9464

Reviewed By: pdillinger

Differential Revision: D33849876

Pulled By: mrambacher

fbshipit-source-id: 7a69177d7c58c81be293fc7ef8e600d47ddbc14b
main
mrambacher 3 years ago committed by Facebook GitHub Bot
parent 5c53b9008f
commit 81ada95bd7
  1. 6
      include/rocksdb/utilities/object_registry.h
  2. 13
      utilities/object_registry.cc

@ -278,6 +278,9 @@ class ObjectRegistry {
static std::shared_ptr<ObjectRegistry> Default();
explicit ObjectRegistry(const std::shared_ptr<ObjectRegistry>& parent)
: parent_(parent) {}
explicit ObjectRegistry(const std::shared_ptr<ObjectLibrary>& library) {
libraries_.push_back(library);
}
std::shared_ptr<ObjectLibrary> AddLibrary(const std::string& id) {
auto library = std::make_shared<ObjectLibrary>(id);
@ -498,9 +501,6 @@ class ObjectRegistry {
void Dump(Logger* logger) const;
private:
explicit ObjectRegistry(const std::shared_ptr<ObjectLibrary>& library) {
libraries_.push_back(library);
}
static std::string ToManagedObjectKey(const std::string& type,
const std::string& id) {
return type + "://" + id;

@ -8,6 +8,7 @@
#include <ctype.h>
#include "logging/logging.h"
#include "port/lang.h"
#include "rocksdb/customizable.h"
#include "rocksdb/env.h"
#include "util/string_util.h"
@ -135,14 +136,18 @@ void ObjectLibrary::Dump(Logger *logger) const {
// Returns the Default singleton instance of the ObjectLibrary
// This instance will contain most of the "standard" registered objects
std::shared_ptr<ObjectLibrary> &ObjectLibrary::Default() {
static std::shared_ptr<ObjectLibrary> instance =
std::make_shared<ObjectLibrary>("default");
// Use avoid destruction here so the default ObjectLibrary will not be
// statically destroyed and long-lived.
STATIC_AVOID_DESTRUCTION(std::shared_ptr<ObjectLibrary>, instance)
(std::make_shared<ObjectLibrary>("default"));
return instance;
}
std::shared_ptr<ObjectRegistry> ObjectRegistry::Default() {
static std::shared_ptr<ObjectRegistry> instance(
new ObjectRegistry(ObjectLibrary::Default()));
// Use avoid destruction here so the default ObjectRegistry will not be
// statically destroyed and long-lived.
STATIC_AVOID_DESTRUCTION(std::shared_ptr<ObjectRegistry>, instance)
(std::make_shared<ObjectRegistry>(ObjectLibrary::Default()));
return instance;
}

Loading…
Cancel
Save