Do not store raw pointer to NameMapper.

While it was "ok" to store raw pointer as, in our use case, the nameMapper
always live longer than object using it; it is not safe to store
raw pointer on object than may be deleted.

All classes storing a NameMapper now store a shared_ptr.
Functions only using the library (as `HumanReadableNameMapper`) continue
to use a (const) reference.
This commit is contained in:
Matthieu Gautier 2022-10-07 11:11:20 +02:00
parent 4842fa0355
commit 3d75f24a29
8 changed files with 15 additions and 15 deletions

View File

@ -42,7 +42,7 @@ class OPDSDumper
{
public:
OPDSDumper() = default;
OPDSDumper(std::shared_ptr<Library> library, NameMapper* NameMapper);
OPDSDumper(std::shared_ptr<Library> library, std::shared_ptr<NameMapper> NameMapper);
~OPDSDumper();
/**
@ -111,7 +111,7 @@ class OPDSDumper
protected:
std::shared_ptr<kiwix::Library> library;
kiwix::NameMapper* nameMapper;
std::shared_ptr<kiwix::NameMapper> nameMapper;
std::string libraryId;
std::string rootLocation;
int m_totalResults;

View File

@ -46,7 +46,7 @@ class SearchRenderer
* @param start The start offset used for the srs.
* @param estimatedResultCount The estimatedResultCount of the whole search
*/
SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper,
SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<NameMapper> mapper,
unsigned int start, unsigned int estimatedResultCount);
/**
@ -58,7 +58,7 @@ class SearchRenderer
* @param start The start offset used for the srs.
* @param estimatedResultCount The estimatedResultCount of the whole search
*/
SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper, std::shared_ptr<Library> library,
SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<NameMapper> mapper, std::shared_ptr<Library> library,
unsigned int start, unsigned int estimatedResultCount);
~SearchRenderer();
@ -106,7 +106,7 @@ class SearchRenderer
protected:
std::string beautifyInteger(const unsigned int number);
zim::SearchResultSet m_srs;
NameMapper* mp_nameMapper;
std::shared_ptr<NameMapper> mp_nameMapper;
std::shared_ptr<Library> mp_library;
std::string searchBookQuery;
std::string searchPattern;

View File

@ -34,7 +34,7 @@ namespace kiwix
public:
class Configuration {
public:
explicit Configuration(std::shared_ptr<Library> library, NameMapper* nameMapper=nullptr)
explicit Configuration(std::shared_ptr<Library> library, std::shared_ptr<NameMapper> nameMapper=nullptr)
: mp_library(library),
mp_nameMapper(nameMapper)
{}
@ -87,7 +87,7 @@ namespace kiwix
}
std::shared_ptr<Library> mp_library;
NameMapper* mp_nameMapper;
std::shared_ptr<NameMapper> mp_nameMapper;
std::string m_root = "";
std::string m_addr = "";
std::string m_indexTemplateString = "";

View File

@ -30,7 +30,7 @@ namespace kiwix
{
/* Constructor */
OPDSDumper::OPDSDumper(std::shared_ptr<Library> library, NameMapper* nameMapper)
OPDSDumper::OPDSDumper(std::shared_ptr<Library> library, std::shared_ptr<NameMapper> nameMapper)
: library(library),
nameMapper(nameMapper)
{

View File

@ -36,12 +36,12 @@ namespace kiwix
{
/* Constructor */
SearchRenderer::SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper,
SearchRenderer::SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<NameMapper> mapper,
unsigned int start, unsigned int estimatedResultCount)
: SearchRenderer(srs, mapper, nullptr, start, estimatedResultCount)
{}
SearchRenderer::SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper, std::shared_ptr<Library> library,
SearchRenderer::SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<NameMapper> mapper, std::shared_ptr<Library> library,
unsigned int start, unsigned int estimatedResultCount)
: m_srs(srs),
mp_nameMapper(mapper),

View File

@ -333,7 +333,7 @@ zim::Query SearchInfo::getZimQuery(bool verbose) const {
return query;
}
static IdNameMapper defaultNameMapper;
static std::shared_ptr<NameMapper> defaultNameMapper = std::make_shared<IdNameMapper>();
static MHD_Result staticHandlerCallback(void* cls,
struct MHD_Connection* connection,
@ -370,7 +370,7 @@ InternalServer::InternalServer(const Server::Configuration& configuration) :
m_configuration(configuration),
m_root(normalizeRootUrl(configuration.m_root)),
m_indexTemplateString(configuration.m_indexTemplateString.empty() ? RESOURCE::templates::index_html : configuration.m_indexTemplateString),
mp_nameMapper(configuration.mp_nameMapper ? configuration.mp_nameMapper : &defaultNameMapper),
mp_nameMapper(configuration.mp_nameMapper ? configuration.mp_nameMapper : defaultNameMapper),
mp_daemon(nullptr),
searchCache(getEnvVar<int>("KIWIX_SEARCH_CACHE_SIZE", DEFAULT_CACHE_SIZE)),
suggestionSearcherCache(getEnvVar<int>("KIWIX_SUGGESTION_SEARCHER_CACHE_SIZE", std::max((unsigned int) (m_configuration.mp_library->getBookCount(true, true)*0.1), 1U))),

View File

@ -153,7 +153,7 @@ class InternalServer {
std::string m_addr;
std::string m_root;
std::string m_indexTemplateString;
NameMapper* mp_nameMapper;
std::shared_ptr<NameMapper> mp_nameMapper;
struct MHD_Daemon* mp_daemon;
SearchCache searchCache;

View File

@ -94,7 +94,7 @@ private:
private: // data
kiwix::Library library;
kiwix::Manager manager;
std::unique_ptr<kiwix::NameMapper> nameMapper;
std::shared_ptr<kiwix::NameMapper> nameMapper;
std::unique_ptr<kiwix::Server> server;
std::unique_ptr<httplib::Client> client;
const Options options = DEFAULT_OPTIONS;
@ -132,7 +132,7 @@ void ZimFileServer::run(int serverPort, std::string indexTemplateString)
} else {
nameMapper.reset(new kiwix::HumanReadableNameMapper(library, false));
}
kiwix::Server::Configuration configuration(NotOwned<kiwix::Library>(library), nameMapper.get());
kiwix::Server::Configuration configuration(NotOwned<kiwix::Library>(library), nameMapper);
configuration.setRoot("ROOT")
.setAddress(address)
.setPort(serverPort)