如何在Android上使用Mapsforge修复原生SIGABRT

前端之家收集整理的这篇文章主要介绍了如何在Android上使用Mapsforge修复原生SIGABRT前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用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 0000000
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,13.38886));
        this.mapView.setZoomLevel((byte) 12);
    }

    @Override
    protected void onDestroy() {
        this.mapView.destroyAll();
        AndroidGraphicFactory.clearResourceMemoryCache();
        super.onDestroy();
    }
}

0000
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())中的free资源无法立即重用,无法在onCreate()中重绘地图.

一种可能的解决方案是从onDestroy()中删除调用AndroidGraphicFactory.clearResourceMemoryCache().正如我所知,这在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进行测试,但我可以在多个设备和操作系统版本上重现此问题.

最佳答案
如果它可以解决你的问题,请试试这个: –

 @Override
      protected void onDestroy() {
        super.onDestroy();
        if (isFinishing()) {
          // do stuff
        } else { 
          //It's an orientation change.
        }
      }
原文链接:https://www.f2er.com/android/430085.html

猜你在找的Android相关文章