[Qt] Share the DefaultFileSource for all QMapboxGL instances

Fix issues of concurrent access to the sqlite cache.

Fixes #9108.
This commit is contained in:
Thiago Marcos P. Santos
2017-07-12 17:01:49 +03:00
parent d0be2ed2a6
commit bb1a209a63
2 changed files with 20 additions and 2 deletions

View File

@@ -70,6 +70,19 @@ namespace {
QThreadStorage<std::shared_ptr<mbgl::util::RunLoop>> loop;
std::shared_ptr<mbgl::DefaultFileSource> sharedDefaultFileSource(
const std::string& cachePath, const std::string& assetRoot, uint64_t maximumCacheSize) {
static std::weak_ptr<mbgl::DefaultFileSource> weak;
auto fs = weak.lock();
if (!fs) {
weak = fs = std::make_shared<mbgl::DefaultFileSource>(
cachePath, assetRoot, maximumCacheSize);
}
return fs;
}
// Conversion helper functions.
mbgl::Size sanitizedSize(const QSize& size) {
@@ -106,6 +119,11 @@ std::unique_ptr<mbgl::style::Image> toStyleImage(const QString &id, const QImage
QMapboxGLSettings is used to configure QMapboxGL at the moment of its creation.
Once created, the QMapboxGLSettings of a QMapboxGL can no longer be changed.
Cache-related settings are shared between all QMapboxGL instances because different
maps will share the same cache database file. The first map to configure cache properties
such as size and path will force the configuration to all newly instantiated QMapboxGL
objects.
\since 4.7
*/
@@ -1473,7 +1491,7 @@ QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settin
: QObject(q)
, size(size_)
, q_ptr(q)
, fileSourceObj(std::make_unique<mbgl::DefaultFileSource>(
, fileSourceObj(sharedDefaultFileSource(
settings.cacheDatabasePath().toStdString(),
settings.assetPath().toStdString(),
settings.cacheDatabaseMaximumSize()))

View File

@@ -52,7 +52,7 @@ public:
QMapboxGL *q_ptr { nullptr };
std::unique_ptr<mbgl::DefaultFileSource> fileSourceObj;
std::shared_ptr<mbgl::DefaultFileSource> fileSourceObj;
std::shared_ptr<mbgl::ThreadPool> threadPool;
std::unique_ptr<mbgl::Map> mapObj;