How to fix native SIGABRT using Mapsforge on Android

1.2k views Asked by At

I'm using Mapsforge 0.6.1 and I can reproduce the issue with the following reduced example, which is basically a copy of this tutorial: https://github.com/mapsforge/mapsforge/blob/master/docs/Getting-Started-Android-App.md

I've 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 app, I have to download http://download.mapsforge.org/maps/europe/germany/berlin.map to the external storage of my device and grant the permission to read it. The app works fine, but after some screen rotations, it crashes.

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)

The problem is, when you rotate the screen, onDestroy() is called, followed by onCreate(). I can reproduce this with more complex apps with multiple activities by switching between the map activity and a different one. Sometimes, resources free'd in AndroidGraphicFactory.clearResourceMemoryCache() (onDestroy()) cannot be reused immediately to redraw the map in onCreate().

A possible solution is to delete the call AndroidGraphicFactory.clearResourceMemoryCache() from onDestroy(). As I figured out, this didn't make any sense at all in Mapsforge 0.6.0, since it has been called in destroyAll() too, deleting that call either would have caused a massive memory leak.

In the current version 0.6.1, using to following method, the error does indeed not occure, because AndroidGraphicFactory.clearResourceMemoryCache() is never called.

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

Although my app seems to work fine, I'm still not convinced, because I think there's still a resource leak. Is this true or is the solution just fine? Is there a better one, maybe a different point in code to open/close AndroidGraphicFactory? Any hints will be highly appreciated.

I'm testing with a Nexus 5X and Android 7.1.1, but I can reproduce this issue on multiple devices and OS versions.

1

There are 1 answers

1
Nitin Singh On

Please try this if it may solve your prob:-

 @Override
      protected void onDestroy() {
        super.onDestroy();
        if (isFinishing()) {
          // do stuff
        } else { 
          //It's an orientation change.
        }
      }