NDK vector.push_back(double_value)最初工作,并在一段时间后崩溃

时间:2018-01-02 07:15:08

标签: android c++ vector memory-management android-ndk

我目前正在开发一个Android项目,我使用矢量来存储从手机的加速计传感器获取的一些实时数据,定期,以每秒20帧的速度存储3-4秒。 该代码适用于前3-4次尝试。我使用以下代码:

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

其中Ax,Ay,Az是从传感器获得的值,使用的矢量定义为:

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

我想我必须释放向量使用的内存。所以我尝试了以下所有情况(并非全部一次)来清除向量/释放所使用的内存,但我仍然得到错误。打印尺寸后,我得到0作为尺寸和容量的值。

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());
    }

如上所述,代码在前3次尝试中正常工作。但之后崩溃了。此外,清除前ax_arr.size()的值在前3次尝试中介于10到100之间,并且在崩溃发生前精确到134086656 。 我在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)

是否有其他方法可以释放所使用的内存或为每次尝试重置整个内存?或者有更好的方法在NDK中进行内存管理吗?

0 个答案:

没有答案