react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

android app crashes

Open Coala1010 opened this issue 3 years ago • 7 comments

Your Environment

  • Plugin version: ^4.1.1
  • Platform: Android
  • OS version: Android 9
  • Device manufacturer / model: Huawei Mate 9
  • React Native version (react-native -v): 0.63.4
  • Plugin config
/**
 * @format
 */

import { AppRegistry } from 'react-native';
import App from './src/App';
import { name as appName } from './app.json';
import BackgroundGeolocation from 'react-native-background-geolocation';
import * as api from '@src/api';
import * as utils from '@src/utils';
import {
  BACKGROUND_UPLOAD_LOCATION_INTERVAL,
  LOCATION_MOVING_DISTANCE,
} from '@src/const';

AppRegistry.registerComponent(appName, () => App);

////
// Define your Headless task -- simply a javascript async function to receive
// events from BackgroundGeolocation:
//
let HeadlessTask = async (event) => {
  let params = event.params;
  console.log('[BackgroundGeolocation HeadlessTask] -', event.name, params);
  switch (event.name) {
    case 'location':
      // #RELIPA_2021/07/06: Only send location when activity isn't stationary.
      if (!params.is_moving && params.activity.type === 'still') {
        return;
      }

      // Use await for async tasks
      let location = await getCurrentPosition();
      console.log(
        '[BackgroundGeolocation HeadlessTask] - getCurrentPosition:',
        location,
      );
      const { coords } = location;
      // #RELIPA_2021/07/05: Check if don't have coords then don't send location to server.
      if (!coords) {
        return;
      }
      const { latitude, longitude, accuracy } = coords;

      const lastUploadedTimestamp = utils.getLastUploadLocationTimestamp();
      const currentTimestamp = new Date().getTime();

      // #RELIPA_2021/07/06: Get last location and compare with current location, will send location to server if the distance between last location saved and current location greater than 0.5 meter.
      const lastLocationSaved = await utils.getCurrentLocation();
      const distanceMoved = utils.distanceBetweenTwoLocation(
        lastLocationSaved.lat,
        lastLocationSaved.lng,
        latitude,
        longitude,
      );
      const isLocationChanged = Boolean(
        !lastLocationSaved || distanceMoved > LOCATION_MOVING_DISTANCE,
      );

      if (
        isLocationChanged &&
        currentTimestamp - lastUploadedTimestamp >=
          BACKGROUND_UPLOAD_LOCATION_INTERVAL
      ) {
        utils.setLastUploadLocationTimestamp(currentTimestamp);
        api.uploadLocation(latitude, longitude);
      }

      utils.saveCurrentLocation(latitude, longitude, accuracy);
      break;
  }
};

////
// You're free to execute any API method upon BackgroundGeolocation in your HeadlessTask.
// Just be sure to wrap them in a Promise and "await" their completion.
//
let getCurrentPosition = () => {
  return new Promise((resolve) => {
    BackgroundGeolocation.getCurrentPosition(
      {
        samples: 1,
        persist: false,
      },
      (location) => {
        resolve(location);
      },
      (error) => {
        resolve(error);
      },
    );
  });
};

////
// Register your HeadlessTask with BackgroundGeolocation plugin.
//
BackgroundGeolocation.registerHeadlessTask(HeadlessTask);

Expected Behavior

The app should work without crash

Actual Behavior

Fatal Exception: java.lang.OutOfMemoryError pthread_create (1040KB stack) failed: Out of memory

Steps to Reproduce

The app often crashes

Context

Debug logs

Logs
Fatal Exception: java.lang.OutOfMemoryError
pthread_create (1040KB stack) failed: Out of memory

com.transistorsoft.locationmanager.logger.TSSQLiteAppender.append (Unknown Source:15)
com.transistorsoft.locationmanager.logger.TSSQLiteAppender.append (Unknown Source:2)
ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend (Unknown Source:84)
ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders (Unknown Source:15)
ch.qos.logback.classic.Logger.appendLoopOnAppenders (Unknown Source:4)
ch.qos.logback.classic.Logger.callAppenders (Unknown Source:4)
ch.qos.logback.classic.Logger.buildLoggingEventAndAppend (Unknown Source:15)
ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus (Unknown Source:28)
ch.qos.logback.classic.Logger.debug (Unknown Source:9)
com.transistorsoft.locationmanager.service.AbstractService.a (Unknown Source:282)
com.transistorsoft.locationmanager.service.LocationRequestService.onStartCommand (Unknown Source:1)

Coala1010 avatar Jul 23 '21 07:07 Coala1010

Let me attach my gradle.properties I extented Max Perm size

# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true

# Version of flipper SDK to use with React Native
FLIPPER_VERSION=0.75.1

Coala1010 avatar Jul 23 '21 07:07 Coala1010

Check your storage space on device.

christocracy avatar Jul 26 '21 13:07 christocracy

I already checked, the free space is enough

Coala1010 avatar Jul 26 '21 13:07 Coala1010

Your error comes when the plugin is attempting to insert a row into its log database. SQLite says "Sorry, no memory".

christocracy avatar Jul 26 '21 13:07 christocracy

I had a similar error but i realized it only happens on android 11 `*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** pid: 0, tid: 0 >>> packageName <<<

backtrace: #00 pc 0000000000062c94 /apex/com.android.runtime/lib/bionic/libc.so (abort+172) #00 pc 000000000003389f /system/lib/libc++.so (abort_message+90) #00 pc 00000000000339fb /system/lib/libc++.so (demangling_terminate_handler()+142) #00 pc 0000000000034353 /system/lib/libc++.so (std::__terminate(void ()())+2) #00 pc 0000000000033bcf /system/lib/libc++.so (__cxxabiv1::failed_throw(__cxxabiv1::__cxa_exception)+12) #00 pc 0000000000033b65 /system/lib/libc++.so (__cxa_throw+72) #00 pc 0000000000036221 /system/lib/libc++.so (operator new(unsigned int)+52) #00 pc 00000000002e0221 /apex/com.android.art/lib/libart.so (art::JNI::GetStringUTFChars(_JNIEnv*, _jstring*, unsigned char*)+508) #00 pc 000000000006a56f /system/lib/libandroid_runtime.so (android::android_util_Log_println_native(_JNIEnv*, _jobject*, int, int, _jstring*, _jstring*)+90) #00 pc 000000000024aecf /system/framework/arm/boot-framework.oat (art_jni_trampoline+158) #00 pc 0000000002003eeb /memfd:jit-cache (android.database.sqlite.SQLiteConnectionPool.logConnectionPoolBusyLocked+2106) #00 pc 00000000003758f3 /system/framework/arm/boot-framework.oat (android.database.sqlite.SQLiteConnectionPool.waitForConnection+1106) #00 pc 0000000000375c8b /system/framework/arm/boot-framework.oat (android.database.sqlite.SQLiteConnectionPool.acquireConnection+58) #00 pc 000000000037dbb7 /system/framework/arm/boot-framework.oat (android.database.sqlite.SQLiteSession.prepare+126) #00 pc 000000000045a6a7 /system/framework/arm/boot-framework.oat (android.database.sqlite.SQLiteProgram.+350) #00 pc 0000000000457f7f /system/framework/arm/boot-framework.oat (android.database.sqlite.SQLiteDatabase.compileStatement+102) #00 pc 000000000060a035 /data/app/~~XV7iaz10ja7U3P0ufacNaQ==/packageName-ivvC6iUneuJ8uc8o4_qCqw==/oat/arm/base.odex (com.transistorsoft.locationmanager.logger.TSSQLiteAppender$b.run+188) #00 pc 000000000023f8b7 /apex/com.android.art/javalib/arm/boot.oat (java.util.concurrent.ThreadPoolExecutor.runWorker+1014) #00 pc 000000000023d107 /apex/com.android.art/javalib/arm/boot.oat (java.util.concurrent.ThreadPoolExecutor$Worker.run+54) #00 pc 000000000012f399 /apex/com.android.art/javalib/arm/boot.oat (java.lang.Thread.run+64) #00 pc 00000000000d47d5 /apex/com.android.art/lib/libart.so (art_quick_invoke_stub_internal+68) #00 pc 00000000004f2427 /apex/com.android.art/lib/libart.so (art_quick_invoke_stub+282) #00 pc 000000000012cb2d /apex/com.android.art/lib/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+148) #00 pc 00000000003ff4df /apex/com.android.art/lib/libart.so (art::JValue art::InvokeVirtualOrInterfaceWithJValuesart::ArtMethod*(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, art::ArtMethod*, jvalue const*)+374) #00 pc 00000000003ff5ef /apex/com.android.art/lib/libart.so (art::JValue art::InvokeVirtualOrInterfaceWithJValues<_jmethodID*>(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jmethodID*, jvalue const*)+42) #00 pc 0000000000440b35 /apex/com.android.art/lib/libart.so (art::Thread::CreateCallback(void*)+1056) #00 pc 00000000000aad13 /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+40) #00 pc 0000000000064193 /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+30)`

Mrllopes avatar Mar 15 '22 21:03 Mrllopes

@Mrllopes Try using the latest version 4.4.6 released to the private repo.

christocracy avatar Mar 16 '22 13:03 christocracy

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

stale[bot] avatar Jun 12 '22 19:06 stale[bot]

Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.

stale[bot] avatar Sep 21 '22 04:09 stale[bot]