0

I am currently working on an android project where I use a vector to store some real-time data acquired from phone's accelerometer sensor, periodically, for 3-4 seconds at the rate of 20 frames per second. The code works fine for the first 3-4 attempts. I am using following code:

ax_arr.push_back(Ax);
ay_arr.push_back(Ay);
az_arr.push_back(Az);

where Ax, Ay, Az are values acquired from sensor and vectors used are defined as:

std::vector<double> ax_arr, ay_arr, az_arr;

I guess I have to deallocate the memory used by the vector. So I tried all the following cases (Not all at once) to clear the vectors/deallocate the memory used, but I still get the error. After printing the size I get 0 as the value for size and capacity.

void clearVectors() {
        LOGD("ax_arr before clear: %i", ax_arr.size());

        std::vector<double >().swap(ax_arr);
        std::vector<double >().swap(ay_arr);
        std::vector<double >().swap(az_arr);

        ax_arr.clear();
        ay_arr.clear();
        az_arr.clear();

        ax_arr.shrink_to_fit();
        ay_arr.shrink_to_fit();
        az_arr.shrink_to_fit();

        delete ax_arr;
        delete ay_arr;
        delete az_arr;

        LOGD("ax_arr after clear: %i, %lu",ax_arr.size(), ax_arr.capacity());
    }

As mentioned, the code works fine for the first 3 attempts. But crashes after it. Also, the value for ax_arr.size() before clearing is between 10 to 100 for the first 3 attempts and shoots up to exactly 134086656 before the crash occurs. I get the following error in android logcat:

01-02 11:58:42.341 A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 2835 (t.ndksensor)

                           [ 01-02 11:58:42.344   190:  190 W/         ]
                           debuggerd: handling request: pid=2835 uid=10055 gid=10055 tid=2835
01-02 11:58:42.573 A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
01-02 11:58:42.574 A/DEBUG: Build fingerprint: 'motorola/eel/carp:7.1.1/NWD1.171020.001/4408697:user/release-keys'
01-02 11:58:42.574 A/DEBUG: Revision: '0'
01-02 11:58:42.574 A/DEBUG: ABI: 'arm'
01-02 11:58:42.575 A/DEBUG: pid: 2835, tid: 2835, name: t.ndksensor  >>> com.nikhil.ndksensor <<<
01-02 11:58:42.575 A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
01-02 11:58:42.575 A/DEBUG:     r0 00000000  r1 00000000  r2 aea0b8b8  r3 00000001
01-02 11:58:42.575 A/DEBUG:     r4 00000000  r5 bff00000  r6 ae997008  r7 00000000
01-02 11:58:42.575 A/DEBUG:     r8 ae997008  r9 ae985400  sl bee28030  fp bff00000
01-02 11:58:42.575 A/DEBUG:     ip 00000001  sp bee27c88  lr 0000000d  pc aeb30fa0  cpsr 600e0030
01-02 11:58:42.614 A/DEBUG: backtrace:
01-02 11:58:42.614 A/DEBUG:     #00 pc 00061fa0  /system/lib/libc.so (je_huge_salloc+7)
01-02 11:58:42.614 A/DEBUG:     #01 pc 0006650f  /system/lib/libc.so (ifree+242)
01-02 11:58:42.614 A/DEBUG:     #02 pc 000668a7  /system/lib/libc.so (je_free+74)
01-02 11:58:42.614 A/DEBUG:     #03 pc 0000ea1b  /data/app/com.nikhil.ndksensor-2/lib/arm/libnative-lib.so (_ZN9__gnu_cxx13new_allocatorIdE10deallocateEPdj+30)
01-02 11:58:42.614 A/DEBUG:     #04 pc 0000e9f5  /data/app/com.nikhil.ndksensor-2/lib/arm/libnative-lib.so (_ZNSt16allocator_traitsISaIdEE10deallocateERS0_Pdj+34)
01-02 11:58:42.614 A/DEBUG:     #05 pc 0000e9af  /data/app/com.nikhil.ndksensor-2/lib/arm/libnative-lib.so (_ZNSt12_Vector_baseIdSaIdEE13_M_deallocateEPdj+46)
01-02 11:58:42.614 A/DEBUG:     #06 pc 0001005d  /data/app/com.nikhil.ndksensor-2/lib/arm/libnative-lib.so (_ZNSt6vectorIdSaIdEE19_M_emplace_back_auxIJRKdEEEvDpOT_+324)
01-02 11:58:42.614 A/DEBUG:     #07 pc 0000f80b  /data/app/com.nikhil.ndksensor-2/lib/arm/libnative-lib.so (_ZNSt6vectorIdSaIdEE9push_backERKd+58)
01-02 11:58:42.615 A/DEBUG:     #08 pc 0000deb1  /data/app/com.nikhil.ndksensor-2/lib/arm/libnative-lib.so (_ZN10native_lib12calculateRepEdddddd+176)
01-02 11:58:42.615 A/DEBUG:     #09 pc 0000dda3  /data/app/com.nikhil.ndksensor-2/lib/arm/libnative-lib.so (Java_com_nikhil_wear_callnative_CallNativeFunctions_calcRep+122)
01-02 11:58:42.615 A/DEBUG:     #10 pc 000ae319  /system/lib/libart.so (art_quick_generic_jni_trampoline+40)
01-02 11:58:42.615 A/DEBUG:     #11 pc 00001b81  /dev/ashmem/dalvik-jit-code-cache (deleted)

Is there any other way to deallocate memory used or to reset the entire memory for every new attempt? Or is there a better way to do the memory-management in NDK?

Nikhil Tambe
  • 502
  • 1
  • 4
  • 13
  • Is this possibly a threading issue requiring a mutex to protect access to the vectors? – SoronelHaetir Jan 02 '18 at 07:22
  • I would not recommend to shrink the vectors. Using **clear()** once in a while to produce empty vector without affecting the *capacity* is most efficient. Bu this must be called on the same thread that listens to sensors. – Alex Cohn Jan 02 '18 at 08:04
  • Note that you can poll the accelerometer in C++. Only make sure you do it on a [separate **HandlerThread**](https://stackoverflow.com/questions/17513352/acclerometer-sensor-in-separate-thread/17513504#17513504). – Alex Cohn Jan 02 '18 at 08:11
  • The `delete` seems out of place, and should not even compile. It requires a pointer to an object created by `new`. – Bo Persson Jan 02 '18 at 09:01

0 Answers0