我正在使用 Mapsforge 0.6.1,我可以通过以下简化示例重现该问题,该示例基本上是本教程的副本:https ://github.com/mapsforge/mapsforge/blob/master/docs/Getting-Started -Android-App.md
我做了一些小改动以匹配新版本。
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();
}
}
要运行该应用程序,我必须将http://download.mapsforge.org/maps/europe/germany/berlin.map下载到我设备的外部存储并授予读取权限。该应用程序运行良好,但经过一些屏幕旋转后,它会崩溃。
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: #10 pc 000000000000135c /data/data/org.test.mapsforgeexample/cache/slice-mapsforge-map-android-0.6.0_24fb6ab0ab8de752356b68dd9111194aa0b25568-classes.dex (offset 0x1c000)
问题是,当您旋转屏幕时,onDestroy()
会调用onCreate()
. 我可以通过在地图活动和不同活动之间切换来使用具有多个活动的更复杂的应用程序来重现这一点。有时,在AndroidGraphicFactory.clearResourceMemoryCache()
( onDestroy()
) 中释放的资源不能立即重新用于在 中重新绘制地图onCreate()
。
一个可能的解决方案是 AndroidGraphicFactory.clearResourceMemoryCache()
从onDestroy()
. 正如我所知道的,这在 Mapsforge 0.6.0 中根本没有任何意义,因为它也已被调用destroyAll()
,删除该调用都会导致大量内存泄漏。
在当前版本 0.6.1 中,使用以下方法,确实没有发生错误,因为AndroidGraphicFactory.clearResourceMemoryCache()
从未调用过。
@Override
protected void onDestroy() {
this.mapView.destroyAll();
super.onDestroy();
}
尽管我的应用程序似乎运行良好,但我仍然不相信,因为我认为仍然存在资源泄漏。这是真的还是解决方案很好?有没有更好的,也许是代码中的不同点来打开/关闭 AndroidGraphicFactory?任何提示将不胜感激。
我正在使用 Nexus 5X 和 Android 7.1.1 进行测试,但我可以在多个设备和操作系统版本上重现此问题。