# HG changeset patch # User Sebastien Jodogne # Date 1617270636 -7200 # Node ID af049cd666612d9ffd044ac2afdf7087f1c441b1 # Parent c8e06b41feeca6fedf7438123388dc99492c697e removed singleton from DatabaseBackendAdapterV3 diff -r c8e06b41feec -r af049cd66661 Framework/Plugins/DatabaseBackendAdapterV3.cpp --- a/Framework/Plugins/DatabaseBackendAdapterV3.cpp Thu Apr 01 11:16:13 2021 +0200 +++ b/Framework/Plugins/DatabaseBackendAdapterV3.cpp Thu Apr 01 11:50:36 2021 +0200 @@ -54,6 +54,8 @@ namespace OrthancDatabases { + static bool isBackendInUse_ = false; // Only for sanity checks + class DatabaseBackendAdapterV3::Output : public IDatabaseBackendOutput { private: @@ -805,13 +807,25 @@ static OrthancPluginErrorCode DestructDatabase(void* database) { - // Nothing to delete, as this plugin uses a singleton to store backend - if (database == NULL) + IndexBackend* backend = reinterpret_cast(database); + + if (backend == NULL) { return OrthancPluginErrorCode_InternalError; } else { + if (isBackendInUse_) + { + isBackendInUse_ = false; + } + else + { + OrthancPluginLogError(backend->GetContext(), "More than one index backend was registered, internal error"); + } + + delete backend; + return OrthancPluginErrorCode_Success; } } @@ -1755,22 +1769,18 @@ } - static std::unique_ptr backend_; - void DatabaseBackendAdapterV3::Register(IndexBackend* backend) { + if (isBackendInUse_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + if (backend == NULL) { throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); } - if (backend_.get() != NULL) - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); - } - - backend_.reset(backend); - OrthancPluginDatabaseBackendV3 params; memset(¶ms, 0, sizeof(params)); @@ -1843,20 +1853,25 @@ params.setProtectedPatient = SetProtectedPatient; params.setResourcesContent = SetResourcesContent; - OrthancPluginContext* context = backend_->GetContext(); + OrthancPluginContext* context = backend->GetContext(); - if (OrthancPluginRegisterDatabaseBackendV3(context, ¶ms, sizeof(params), backend_.get()) != OrthancPluginErrorCode_Success) + if (OrthancPluginRegisterDatabaseBackendV3(context, ¶ms, sizeof(params), backend) != OrthancPluginErrorCode_Success) { throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Unable to register the database backend"); } - backend_->SetOutputFactory(new Factory); + backend->SetOutputFactory(new Factory); + + isBackendInUse_ = true; } void DatabaseBackendAdapterV3::Finalize() { - backend_.reset(NULL); + if (isBackendInUse_) + { + fprintf(stderr, "The Orthanc core has not destructed the index backend, internal error\n"); + } } }