I am using Mapsforge 0.6.1 and I can reproduce the problem with the following example, which is basically a copy of this tutorial: https://github.com/mapsforge/mapsforge/blob/master/docs/Getting-Started-Android-App .md
I made some minor changes to match the new version.
package org.test.mapsforgeexample; import java.io.File; import org.mapsforge.core.model.LatLong; import org.mapsforge.map.android.graphics.AndroidGraphicFactory; import org.mapsforge.map.android.util.AndroidUtil; import org.mapsforge.map.android.view.MapView; import org.mapsforge.map.layer.cache.TileCache; import org.mapsforge.map.layer.renderer.TileRendererLayer; import org.mapsforge.map.datastore.MapDataStore; import org.mapsforge.map.reader.MapFile; import org.mapsforge.map.rendertheme.InternalRenderTheme; import android.app.Activity; import android.os.Bundle; import android.os.Environment; public class MainActivity extends Activity { private static final String MAP_FILE = "berlin.map"; private MapView mapView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidGraphicFactory.createInstance(this.getApplication()); this.mapView = new MapView(this); setContentView(this.mapView); this.mapView.setClickable(true); this.mapView.getMapScaleBar().setVisible(true); this.mapView.setBuiltInZoomControls(true); this.mapView.getMapZoomControls().setZoomLevelMin((byte) 10); this.mapView.getMapZoomControls().setZoomLevelMax((byte) 20); TileCache tileCache = AndroidUtil.createTileCache(this, "mapcache", mapView.getModel().displayModel.getTileSize(), 1f, this.mapView.getModel().frameBufferModel.getOverdrawFactor()); MapDataStore mapDataStore = new MapFile(new File(Environment.getExternalStorageDirectory(), MAP_FILE)); TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, this.mapView.getModel().mapViewPosition, AndroidGraphicFactory.INSTANCE); tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.OSMARENDER); this.mapView.getLayerManager().getLayers().add(tileRendererLayer); this.mapView.setCenter(new LatLong(52.517037, 13.38886)); this.mapView.setZoomLevel((byte) 12); } @Override protected void onDestroy() { this.mapView.destroyAll(); AndroidGraphicFactory.clearResourceMemoryCache(); super.onDestroy(); } }
To run the application, I need to download http://download.mapsforge.org/maps/europe/germany/berlin.map to the external memory of my device and provide permission to read it. The application works fine, but after some screen turns it will work.
12-16 11:43:34.055 1496-1496/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 12-16 11:43:34.056 1496-1496/? A/DEBUG: Build fingerprint: 'google/bullhead/bullhead:7.1.1/NMF26F/3425388:user/release-keys' 12-16 11:43:34.056 1496-1496/? A/DEBUG: Revision: 'rev_1.0' 12-16 11:43:34.056 1496-1496/? A/DEBUG: ABI: 'arm64' 12-16 11:43:34.056 1496-1496/? A/DEBUG: pid: 1425, tid: 1449, name: pool-4-thread-1 >>> org.test.mapsforgeexample <<< 12-16 11:43:34.056 1496-1496/? A/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr -------- 12-16 11:43:34.058 1496-1496/? A/DEBUG: Abort message: 'Error, cannot access an invalid/free'd bitmap here!' 12-16 11:43:34.058 1496-1496/? A/DEBUG: x0 0000000000000000 x1 00000000000005a9 x2 0000000000000006 x3 0000000000000008 12-16 11:43:34.058 1496-1496/? A/DEBUG: x4 0000000000000000 x5 0000000000000000 x6 00000078e82a2000 x7 0000000000000000 12-16 11:43:34.058 1496-1496/? A/DEBUG: x8 0000000000000083 x9 ffffffffffffffdf x10 0000000000000000 x11 0000000000000001 12-16 11:43:34.058 1496-1496/? A/DEBUG: x12 0000000000000018 x13 0000000000000000 x14 0000000000000000 x15 003627f486f8cd75 12-16 11:43:34.058 1496-1496/? A/DEBUG: x16 00000078e72d5ee0 x17 00000078e727faac x18 0000000007e0f81f x19 00000078c97ff4f8 12-16 11:43:34.058 1496-1496/? A/DEBUG: x20 0000000000000006 x21 00000078c97ff450 x22 000000000000000b x23 00000000138bedd0 12-16 11:43:34.058 1496-1496/? A/DEBUG: x24 00000000138c1300 x25 00000078d79047e0 x26 00000078c8e4c720 x27 0000000013b99b98 12-16 11:43:34.058 1496-1496/? A/DEBUG: x28 00000000138c12f0 x29 00000078c97fe320 x30 00000078e727ced8 12-16 11:43:34.058 1496-1496/? A/DEBUG: sp 00000078c97fe300 pc 00000078e727fab4 pstate 0000000060000000 12-16 11:43:34.549 1496-1496/? A/DEBUG: backtrace: 12-16 11:43:34.549 1496-1496/? A/DEBUG: #00 pc 000000000006bab4 /system/lib64/libc.so (tgkill+8) 12-16 11:43:34.549 1496-1496/? A/DEBUG: #01 pc 0000000000068ed4 /system/lib64/libc.so (pthread_kill+64) 12-16 11:43:34.549 1496-1496/? A/DEBUG: #02 pc 0000000000023f58 /system/lib64/libc.so (raise+24) 12-16 11:43:34.549 1496-1496/? A/DEBUG: #03 pc 000000000001c810 /system/lib64/libc.so (abort+52) 12-16 11:43:34.549 1496-1496/? A/DEBUG: #04 pc 0000000000010c24 /system/lib64/libcutils.so (__android_log_assert+224) 12-16 11:43:34.549 1496-1496/? A/DEBUG: #05 pc 00000000001073e4 /system/lib64/libandroid_runtime.so (_ZNK7android6Bitmap11assertValidEv+40) 12-16 11:43:34.549 1496-1496/? A/DEBUG: #06 pc 00000000001074c8 /system/lib64/libandroid_runtime.so (_ZN7android6Bitmap11getSkBitmapEP8SkBitmap+20) 12-16 11:43:34.550 1496-1496/? A/DEBUG: #07 pc 000000000010359c /system/lib64/libandroid_runtime.so 12-16 11:43:34.550 1496-1496/? A/DEBUG: #08 pc 0000000001a9a3a0 /system/framework/arm64/boot-framework.oat (offset 0x1686000) (android.graphics.Canvas.nativeDrawBitmapMatrix+172) 12-16 11:43:34.550 1496-1496/? A/DEBUG: #09 pc 0000000001a9e9b8 /system/framework/arm64/boot-framework.oat (offset 0x1686000) (android.graphics.Canvas.drawBitmap+148) 12-16 11:43:34.550 1496-1496/? A/DEBUG:
The problem is that when you rotate the screen, onDestroy() is onDestroy() , and then onCreate() . I can reproduce this with more complex applications with several actions, switching the activity of the card and the other. Sometimes resources free from AndroidGraphicFactory.clearResourceMemoryCache() ( onDestroy() ) cannot be reused to redraw the map in onCreate() .
A possible solution is to remove the call to AndroidGraphicFactory.clearResourceMemoryCache() from onDestroy() . As I understand it, this made no sense in Mapsforge 0.6.0, since it was called in destroyAll() too, deleting this call would either cause a large memory leak.
In current version 0.6.1, using the following method, the error does not really occur, because AndroidGraphicFactory.clearResourceMemoryCache() never called.
@Override protected void onDestroy() { this.mapView.destroyAll(); super.onDestroy(); }
Although my application works fine, I'm still not convinced because I think there is still a resource leak. Is this true or is this solution just fine? Is there any better, maybe different code in the code for opening / closing AndroidGraphicFactory? Any hints would be greatly appreciated.
I am testing Nexus 5X and Android 7.1.1, but I can reproduce this problem on several devices and OS versions.