@@ -126,10 +126,10 @@ void QGCCachedTileSet::_doneWithDownload()
126126 setTotalTileSize (_savedTileSize);
127127
128128 quint32 avg = 0 ;
129- if (_savedTileSize != 0 ) {
129+ if (_savedTileCount != 0 ) {
130130 avg = _savedTileSize / _savedTileCount;
131131 } else {
132- qCWarning (QGCCachedTileSetLog) << " _savedTileSize =0" ;
132+ qCWarning (QGCCachedTileSetLog) << " _savedTileCount =0" ;
133133 }
134134
135135 setUniqueTileSize (_uniqueTileCount * avg);
@@ -151,11 +151,22 @@ void QGCCachedTileSet::_prepareDownload()
151151 return ;
152152 }
153153
154- for (qsizetype i = _replies.count (); i < QGeoTileFetcherQGC::concurrentDownloads (_type); i++) {
154+ const qsizetype maxConcurrent = QGeoTileFetcherQGC::concurrentDownloads (_type);
155+
156+ for (qsizetype i = 0 ; i < maxConcurrent; i++) {
155157 if (_tilesToDownload.isEmpty ()) {
156158 break ;
157159 }
158160
161+ // Check if we've reached the concurrent limit
162+ {
163+ QMutexLocker lock (&_repliesMutex);
164+ if (_replies.count () >= maxConcurrent) {
165+ break ;
166+ }
167+ }
168+
169+ // Dequeue and prepare tile request without holding the mutex
159170 QGCTile* const tile = _tilesToDownload.dequeue ();
160171 const int mapId = UrlFactory::getQtMapIdFromProviderType (tile->type );
161172 QNetworkRequest request = QGeoTileFetcherQGC::getNetworkRequest (mapId, tile->x , tile->y , tile->z );
@@ -167,10 +178,15 @@ void QGCCachedTileSet::_prepareDownload()
167178 QGCFileDownload::setIgnoreSSLErrorsIfNeeded (*reply);
168179 (void ) connect (reply, &QNetworkReply::finished, this , &QGCCachedTileSet::_networkReplyFinished);
169180 (void ) connect (reply, &QNetworkReply::errorOccurred, this , &QGCCachedTileSet::_networkReplyError);
170- (void ) _replies.insert (tile->hash , reply);
181+
182+ // Insert into replies map
183+ {
184+ QMutexLocker lock (&_repliesMutex);
185+ (void ) _replies.insert (tile->hash , reply);
186+ }
171187
172188 delete tile;
173- if (!_batchRequested && !_noMoreTiles && (_tilesToDownload.count () < (QGeoTileFetcherQGC::concurrentDownloads (_type) * 10 ))) {
189+ if (!_batchRequested && !_noMoreTiles && (_tilesToDownload.count () < (maxConcurrent * 10 ))) {
174190 createDownloadTask ();
175191 }
176192 }
@@ -200,10 +216,13 @@ void QGCCachedTileSet::_networkReplyFinished()
200216 return ;
201217 }
202218
203- if (_replies.contains (hash)) {
204- (void ) _replies.remove (hash);
205- } else {
206- qCWarning (QGCCachedTileSetLog) << " Reply not in list: " << hash;
219+ {
220+ QMutexLocker lock (&_repliesMutex);
221+ if (_replies.contains (hash)) {
222+ (void ) _replies.remove (hash);
223+ } else {
224+ qCWarning (QGCCachedTileSetLog) << " Reply not in list: " << hash;
225+ }
207226 }
208227 qCDebug (QGCCachedTileSetLog) << " Tile fetched:" << hash;
209228
@@ -215,7 +234,10 @@ void QGCCachedTileSet::_networkReplyFinished()
215234
216235 const QString type = UrlFactory::tileHashToType (hash);
217236 const SharedMapProvider mapProvider = UrlFactory::getMapProviderFromProviderType (type);
218- Q_CHECK_PTR (mapProvider);
237+ if (!mapProvider) {
238+ qCWarning (QGCCachedTileSetLog) << " Invalid map provider for type:" << type;
239+ return ;
240+ }
219241
220242 if (mapProvider->isElevationProvider ()) {
221243 const SharedElevationProvider elevationProvider = std::dynamic_pointer_cast<const ElevationProvider>(mapProvider);
@@ -242,7 +264,7 @@ void QGCCachedTileSet::_networkReplyFinished()
242264 setSavedTileSize (_savedTileSize + image.size ());
243265 setSavedTileCount (_savedTileCount + 1 );
244266
245- if (_savedTileCount % 10 == 0 ) {
267+ if (_savedTileCount % kSizeEstimateInterval == 0 ) {
246268 const quint32 avg = _savedTileSize / _savedTileCount;
247269 setTotalTileSize (avg * _totalTileCount);
248270 setUniqueTileSize (avg * _uniqueTileCount);
@@ -267,10 +289,13 @@ void QGCCachedTileSet::_networkReplyError(QNetworkReply::NetworkError error)
267289 return ;
268290 }
269291
270- if (_replies.contains (hash)) {
271- (void ) _replies.remove (hash);
272- } else {
273- qCWarning (QGCCachedTileSetLog) << " Reply not in list:" << hash;
292+ {
293+ QMutexLocker lock (&_repliesMutex);
294+ if (_replies.contains (hash)) {
295+ (void ) _replies.remove (hash);
296+ } else {
297+ qCWarning (QGCCachedTileSetLog) << " Reply not in list:" << hash;
298+ }
274299 }
275300
276301 if (error != QNetworkReply::OperationCanceledError) {
0 commit comments