Repository navigation
Description
Summary
A TileRendererLayer that is removed from Layers and added again never renders another tile. TileLayer.setDisplayModel() creates a fresh JobQueue on every call, but TileRendererLayer.setDisplayModel() creates its MapWorkerPool only once, bound to the first queue. After the re-add, TileLayer.draw() queues jobs into the new queue while the worker pool waits on the old one forever.
Affects 0.30.0 and current master.
Mechanism
Layers.add() calls layer.setDisplayModel(displayModel) on every add.
TileLayer.setDisplayModel() (TileLayer.java:188):
super.setDisplayModel(displayModel);
if (displayModel != null && this.hasJobQueue) {
this.jobQueue = new JobQueue<>(this.mapViewPosition, this.displayModel); // new queue each time
}TileRendererLayer.setDisplayModel() (TileRendererLayer.java:135):
super.setDisplayModel(displayModel);
if (displayModel != null) {
compileRenderTheme();
if (this.mapWorkerPool == null) { // only the first time
this.mapWorkerPool = new MapWorkerPool(this.tileCache, this.jobQueue, this.databaseRenderer, this);
}
this.mapWorkerPool.start();
}MapWorkerPool.jobQueue is final, so after the second setDisplayModel() the pool's run() loop blocks in JobQueue.get() on the stale queue and never spawns a worker, while draw() fills this.jobQueue (the new one). A thread dump shows the pool loop in Object.wait() inside JobQueue.get, and a heap histogram shows two JobQueue instances for the layer.
Reproduction (headless, AWT)
Package org.mapsforge.map.layer only because the Layers constructor is package-private.
package org.mapsforge.map.layer;
import org.mapsforge.core.graphics.*;
import org.mapsforge.core.model.*;
import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
import org.mapsforge.map.layer.cache.InMemoryTileCache;
import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.model.*;
import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.internal.MapsforgeThemes;
import org.mapsforge.map.util.MapPositionUtil;
import java.io.File;
public class ReAddRepro {
public static void main(String[] args) throws Exception {
GraphicFactory gf = AwtGraphicFactory.INSTANCE;
DisplayModel dm = new DisplayModel();
MapViewPosition mvp = new MapViewPosition(dm);
mvp.setMapPosition(new MapPosition(new LatLong(54.0, 10.0), (byte) 7));
InMemoryTileCache cache = new InMemoryTileCache(64);
TileRendererLayer layer = new TileRendererLayer(cache, new MapFile(new File(args[0])), mvp, true, true, true, gf, null);
layer.setXmlRenderTheme(MapsforgeThemes.OSMARENDER);
Layers layers = new Layers(() -> {}, dm);
layers.add(0, layer);
if (args.length > 1) { // pass any 2nd arg to trigger the bug
layers.remove(layer);
layers.add(0, layer);
}
Dimension dim = new Dimension(1024, 768);
MapPosition mp = mvp.getMapPosition();
BoundingBox bbox = MapPositionUtil.getBoundingBox(mp, Rotation.NULL_ROTATION, dm.getTileSize(), dim, 0.5f, 0.5f);
Point tl = MapPositionUtil.getTopLeftPoint(mp, dim, dm.getTileSize());
Canvas canvas = gf.createCanvas();
canvas.setBitmap(gf.createBitmap(dim.width, dim.height));
for (int i = 0; i < 3; i++) {
layer.draw(bbox, mp.zoomLevel, canvas, tl, Rotation.NULL_ROTATION);
Thread.sleep(2000);
System.out.println("draw#" + (i + 1) + " tiles in cache: " + cache.getCapacity() /* replace with your own counting */);
}
System.exit(0);
}
}With any .map file, single add renders all ~20 visible tiles within the first two seconds. With the remove + add in between, nothing is ever rendered (I counted files in a FileSystemTileCache as second level: 20 vs 0, reproducible every run).
Real-world trigger
Any app that reorders layers with Layers.remove() + Layers.add(index, …) (for example to keep a background map below a newly added map layer) silently kills the reordered TileRendererLayer. In RouteConverter this left the world-map background blank for the whole session (cpesch/RouteConverter#376).
Suggested fix
Either create the JobQueue once (in the TileLayer constructor or only when it is null), or in TileRendererLayer.setDisplayModel() stop and drop the existing MapWorkerPool when the queue changes so a new pool is bound to the current queue. TileDownloadLayer does not have the problem because it recreates its TileDownloadThreads with the current jobQueue on every setDisplayModel().
Workaround used in RouteConverter: a subclass that remembers the first jobQueue and restores it after every super.setDisplayModel().
Activity
test(mapsforge-mapview): TileRendererLayer lifecycle test for remove/…
devemux86 commentedon Oct 9, 2026
@cpesch Thanks for the report.
If you made the improvement in your code and it works, can you create a pull request?
devemux86 commentedon Oct 19, 2026
@cpesch See #1825