mirror of https://github.com/kiwix/libkiwix.git
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:
parent
4842fa0355
commit
3d75f24a29
|
@ -42,7 +42,7 @@ class OPDSDumper
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OPDSDumper() = default;
|
OPDSDumper() = default;
|
||||||
OPDSDumper(std::shared_ptr<Library> library, NameMapper* NameMapper);
|
OPDSDumper(std::shared_ptr<Library> library, std::shared_ptr<NameMapper> NameMapper);
|
||||||
~OPDSDumper();
|
~OPDSDumper();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,7 +111,7 @@ class OPDSDumper
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::shared_ptr<kiwix::Library> library;
|
std::shared_ptr<kiwix::Library> library;
|
||||||
kiwix::NameMapper* nameMapper;
|
std::shared_ptr<kiwix::NameMapper> nameMapper;
|
||||||
std::string libraryId;
|
std::string libraryId;
|
||||||
std::string rootLocation;
|
std::string rootLocation;
|
||||||
int m_totalResults;
|
int m_totalResults;
|
||||||
|
|
|
@ -46,7 +46,7 @@ class SearchRenderer
|
||||||
* @param start The start offset used for the srs.
|
* @param start The start offset used for the srs.
|
||||||
* @param estimatedResultCount The estimatedResultCount of the whole search
|
* @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);
|
unsigned int start, unsigned int estimatedResultCount);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,7 +58,7 @@ class SearchRenderer
|
||||||
* @param start The start offset used for the srs.
|
* @param start The start offset used for the srs.
|
||||||
* @param estimatedResultCount The estimatedResultCount of the whole search
|
* @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);
|
unsigned int start, unsigned int estimatedResultCount);
|
||||||
|
|
||||||
~SearchRenderer();
|
~SearchRenderer();
|
||||||
|
@ -106,7 +106,7 @@ class SearchRenderer
|
||||||
protected:
|
protected:
|
||||||
std::string beautifyInteger(const unsigned int number);
|
std::string beautifyInteger(const unsigned int number);
|
||||||
zim::SearchResultSet m_srs;
|
zim::SearchResultSet m_srs;
|
||||||
NameMapper* mp_nameMapper;
|
std::shared_ptr<NameMapper> mp_nameMapper;
|
||||||
std::shared_ptr<Library> mp_library;
|
std::shared_ptr<Library> mp_library;
|
||||||
std::string searchBookQuery;
|
std::string searchBookQuery;
|
||||||
std::string searchPattern;
|
std::string searchPattern;
|
||||||
|
|
|
@ -34,7 +34,7 @@ namespace kiwix
|
||||||
public:
|
public:
|
||||||
class Configuration {
|
class Configuration {
|
||||||
public:
|
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_library(library),
|
||||||
mp_nameMapper(nameMapper)
|
mp_nameMapper(nameMapper)
|
||||||
{}
|
{}
|
||||||
|
@ -87,7 +87,7 @@ namespace kiwix
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Library> mp_library;
|
std::shared_ptr<Library> mp_library;
|
||||||
NameMapper* mp_nameMapper;
|
std::shared_ptr<NameMapper> mp_nameMapper;
|
||||||
std::string m_root = "";
|
std::string m_root = "";
|
||||||
std::string m_addr = "";
|
std::string m_addr = "";
|
||||||
std::string m_indexTemplateString = "";
|
std::string m_indexTemplateString = "";
|
||||||
|
|
|
@ -30,7 +30,7 @@ namespace kiwix
|
||||||
{
|
{
|
||||||
|
|
||||||
/* Constructor */
|
/* Constructor */
|
||||||
OPDSDumper::OPDSDumper(std::shared_ptr<Library> library, NameMapper* nameMapper)
|
OPDSDumper::OPDSDumper(std::shared_ptr<Library> library, std::shared_ptr<NameMapper> nameMapper)
|
||||||
: library(library),
|
: library(library),
|
||||||
nameMapper(nameMapper)
|
nameMapper(nameMapper)
|
||||||
{
|
{
|
||||||
|
|
|
@ -36,12 +36,12 @@ namespace kiwix
|
||||||
{
|
{
|
||||||
|
|
||||||
/* Constructor */
|
/* Constructor */
|
||||||
SearchRenderer::SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper,
|
SearchRenderer::SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<NameMapper> mapper,
|
||||||
unsigned int start, unsigned int estimatedResultCount)
|
unsigned int start, unsigned int estimatedResultCount)
|
||||||
: SearchRenderer(srs, mapper, nullptr, start, 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)
|
unsigned int start, unsigned int estimatedResultCount)
|
||||||
: m_srs(srs),
|
: m_srs(srs),
|
||||||
mp_nameMapper(mapper),
|
mp_nameMapper(mapper),
|
||||||
|
|
|
@ -333,7 +333,7 @@ zim::Query SearchInfo::getZimQuery(bool verbose) const {
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
static IdNameMapper defaultNameMapper;
|
static std::shared_ptr<NameMapper> defaultNameMapper = std::make_shared<IdNameMapper>();
|
||||||
|
|
||||||
static MHD_Result staticHandlerCallback(void* cls,
|
static MHD_Result staticHandlerCallback(void* cls,
|
||||||
struct MHD_Connection* connection,
|
struct MHD_Connection* connection,
|
||||||
|
@ -370,7 +370,7 @@ InternalServer::InternalServer(const Server::Configuration& configuration) :
|
||||||
m_configuration(configuration),
|
m_configuration(configuration),
|
||||||
m_root(normalizeRootUrl(configuration.m_root)),
|
m_root(normalizeRootUrl(configuration.m_root)),
|
||||||
m_indexTemplateString(configuration.m_indexTemplateString.empty() ? RESOURCE::templates::index_html : configuration.m_indexTemplateString),
|
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),
|
mp_daemon(nullptr),
|
||||||
searchCache(getEnvVar<int>("KIWIX_SEARCH_CACHE_SIZE", DEFAULT_CACHE_SIZE)),
|
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))),
|
suggestionSearcherCache(getEnvVar<int>("KIWIX_SUGGESTION_SEARCHER_CACHE_SIZE", std::max((unsigned int) (m_configuration.mp_library->getBookCount(true, true)*0.1), 1U))),
|
||||||
|
|
|
@ -153,7 +153,7 @@ class InternalServer {
|
||||||
std::string m_addr;
|
std::string m_addr;
|
||||||
std::string m_root;
|
std::string m_root;
|
||||||
std::string m_indexTemplateString;
|
std::string m_indexTemplateString;
|
||||||
NameMapper* mp_nameMapper;
|
std::shared_ptr<NameMapper> mp_nameMapper;
|
||||||
struct MHD_Daemon* mp_daemon;
|
struct MHD_Daemon* mp_daemon;
|
||||||
|
|
||||||
SearchCache searchCache;
|
SearchCache searchCache;
|
||||||
|
|
|
@ -94,7 +94,7 @@ private:
|
||||||
private: // data
|
private: // data
|
||||||
kiwix::Library library;
|
kiwix::Library library;
|
||||||
kiwix::Manager manager;
|
kiwix::Manager manager;
|
||||||
std::unique_ptr<kiwix::NameMapper> nameMapper;
|
std::shared_ptr<kiwix::NameMapper> nameMapper;
|
||||||
std::unique_ptr<kiwix::Server> server;
|
std::unique_ptr<kiwix::Server> server;
|
||||||
std::unique_ptr<httplib::Client> client;
|
std::unique_ptr<httplib::Client> client;
|
||||||
const Options options = DEFAULT_OPTIONS;
|
const Options options = DEFAULT_OPTIONS;
|
||||||
|
@ -132,7 +132,7 @@ void ZimFileServer::run(int serverPort, std::string indexTemplateString)
|
||||||
} else {
|
} else {
|
||||||
nameMapper.reset(new kiwix::HumanReadableNameMapper(library, false));
|
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")
|
configuration.setRoot("ROOT")
|
||||||
.setAddress(address)
|
.setAddress(address)
|
||||||
.setPort(serverPort)
|
.setPort(serverPort)
|
||||||
|
|
Loading…
Reference in New Issue