gpt4 book ai didi

c++ - NDK 问题 : crash on 32bit, 在 64 位上不一致

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:52 27 4
gpt4 key购买 nike

我的 C++ 代码有这个问题。这是一个图形搜索,它使用多个线程。线程彼此不依赖,它们各自有不同的搜索参数,但将访问只读源图,只写结果数组以及读写作业队列。

32 位 设备上:arm 或 x86 它会在新调用时随机崩溃。是 struct , class , std::vector<>或者当 std::vector<>将不得不重新分配内存以允许更大的容量。

这是一个示例:

********** Crash dump: **********
Build fingerprint: 'google/hammerhead/hammerhead:6.0.1/M4B30Z/3437181:user/release-keys'
pid: 2341, tid: 2356, name: roidJUnitRunner >>> com.transit102.nativesearchengine.test <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xdeadcab1
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #00 pc 00016fce /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so: Routine __gabixx::__default_terminate() at /usr/local/google/buildbot/src/android/ndk-r14-release/out/build/tmp/build-22588/build-stlport/ndk/sources/cxx-stl/gabi++/src/terminate.cc:72
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #01 pc 00016fe3 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so: Routine __gabixx::__terminate(void (*)()) at /usr/local/google/buildbot/src/android/ndk-r14-release/out/build/tmp/build-22588/build-stlport/ndk/sources/cxx-stl/gabi++/src/terminate.cc:84
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #02 pc 00016fb9 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so: Routine std::terminate() at /usr/local/google/buildbot/src/android/ndk-r14-release/out/build/tmp/build-22588/build-stlport/ndk/sources/cxx-stl/gabi++/src/terminate.cc:110 (discriminator 1)
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #03 pc 00016745 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so: Routine __cxxabiv1::call_terminate(_Unwind_Control_Block*) at /usr/local/google/buildbot/src/android/ndk-r14-release/out/build/tmp/build-22588/build-stlport/ndk/sources/cxx-stl/gabi++/src/helper_func_internal.cc:54
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #04 pc 000162dd /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so: Routine (anonymous namespace)::throwException(__cxxabiv1::__cxa_exception*) at /usr/local/google/buildbot/src/android/ndk-r14-release/out/build/tmp/build-22588/build-stlport/ndk/sources/cxx-stl/gabi++/src/cxxabi.cc:271
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #05 pc 000162a3 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so: Routine __cxa_throw at /usr/local/google/buildbot/src/android/ndk-r14-release/out/build/tmp/build-22588/build-stlport/ndk/sources/cxx-stl/gabi++/src/cxxabi.cc:335
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #06 pc 00016cab /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so (_Znwj+70): Routine operator new(unsigned int) at /usr/local/google/buildbot/src/android/ndk-r14-release/out/build/tmp/build-22588/build-stlport/ndk/sources/cxx-stl/gabi++/src/new.cc:105
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #07 pc 0000e0a5 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so (_ZSt9__stl_newj+12): Routine std::__stl_new(unsigned int) at C:/Users/auras/Android/sdk/ndk-bundle/sources/cxx-stl/stlport/stlport\stl/_new.h:134
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #08 pc 0000e081 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so (_ZNSt12__node_alloc8allocateERj+24): Routine std::__node_alloc::allocate(unsigned int&) at C:/Users/auras/Android/sdk/ndk-bundle/sources/cxx-stl/stlport/stlport\stl/_alloc.h:158 (discriminator 1)
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #09 pc 0000e6f3 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so (_ZNSaIjE11_M_allocateEjRj+102): Routine std::allocator<unsigned int>::_M_allocate(unsigned int, unsigned int&) at C:/Users/auras/Android/sdk/ndk-bundle/sources/cxx-stl/stlport/stlport\stl/_alloc.h:348
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #10 pc 0000e685 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so (_ZNSt4priv17_STLP_alloc_proxyIPjjSaIjEE8allocateEjRjRKSt11__true_type+40): Routine std::priv::_STLP_alloc_proxy<unsigned int*, unsigned int, std::allocator<unsigned int> >::allocate(unsigned int, unsigned int&, std::__true_type const&) at C:/Users/auras/Android/sdk/ndk-bundle/sources/cxx-stl/stlport/stlport\stl/_alloc.h:551
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #11 pc 0000e4f1 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so (_ZNSt4priv17_STLP_alloc_proxyIPjjSaIjEE8allocateEjRj+48): Routine std::priv::_STLP_alloc_proxy<unsigned int*, unsigned int, std::allocator<unsigned int> >::allocate(unsigned int, unsigned int&) at C:/Users/auras/Android/sdk/ndk-bundle/sources/cxx-stl/stlport/stlport\stl/_alloc.h:531
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #12 pc 00014c69 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so (_ZNSt4priv12_Vector_baseIjSaIjEEC2EjRKS1_+80): Routine _Vector_base at C:/Users/auras/Android/sdk/ndk-bundle/sources/cxx-stl/stlport/stlport\stl/_vector.h:71
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #13 pc 0001299f /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so (_ZNSt6vectorIjSaIjEEC2ERKS1_+66): Routine vector at C:/Users/auras/Android/sdk/ndk-bundle/sources/cxx-stl/stlport/stlport\stl/_vector.h:247
Stack frame 03-19 20:44:41.379 2142 2142 F DEBUG : #14 pc 0000ff61 /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so (_ZN10transit10215NewSearchEngine7PushJobEjPSt6vectorIjSaIjEEjS4_jS4_hPS1_IPNS_7JourneyESaIS6_EE+240): Routine transit102::NewSearchEngine::PushJob(unsigned int, std::vector<unsigned int, std::allocator<unsigned int> >*, unsigned int, std::vector<unsigned int, std::allocator<unsigned int> >*, unsigned int, std::vector<unsigned int, std::allocator<unsigned int> >*, unsigned char, std::vector<transit102::Journey*, std::allocator<transit102::Journey*> >*) at C:\Users\auras\AndroidStudioProjects\Transit102\nativesearchengine\src\main\jni/newsearchengine.cpp:965

崩溃的共同点总是

Stack frame 03-19 20:44:41.379  2142  2142 F DEBUG   :     #06 pc 00016cab  /data/app/com.transit102.nativesearchengine.test-1/lib/arm/libtransit-searchengine.so (_Znwj+70): Routine operator new(unsigned int) at /usr/local/google/buildbot/src/android/ndk-r14-release/out/build/tmp/build-22588/build-stlport/ndk/sources/cxx-stl/gabi++/src/new.cc:105

64 位 设备上:arm 或 x64 它永远不会崩溃,即使我每次运行测试时都会得到不同的结果,而这在重构代码之前并没有发生。

我的主要问题是崩溃,之后我可以调试,如果它仍然发生,为什么我每次运行都会得到不同的结果。

编辑:

我已将代码移植到 Visual Studio 2015,代码在 32 位或 64 位上从未崩溃,也没有内存泄漏。

CMakeLists.txt:

# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.

cmake_minimum_required(VERSION 3.4.1)

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

add_library( # Specifies the name of the library.
transit-searchengine

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
src/main/jni/android_osutils.cpp
src/main/jni/graph.cpp
src/main/jni/journey.cpp
src/main/jni/newsearchengine.cpp
src/main/jni/searchengine.cpp
src/main/jni/jniwrapper.cpp )

# Specifies a path to native header files.
include_directories(src/main/jni/)

find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib

# Specifies the name of the NDK library that
# CMake needs to locate.
log )

# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
transit-searchengine

# Links the log library to the target library.
${log-lib} )

build.gradle:

android {
compileSdkVersion rootProject.versionAndroidSdk
buildToolsVersion rootProject.versionBuildTools

defaultConfig {
minSdkVersion rootProject.versionMinAndroidSdk
targetSdkVersion rootProject.versionAndroidSdk
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

externalNativeBuild {
cmake {
// Passes optional arguments to CMake.
arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_STL=stlport_static", "-DANDROID_UNIFIED_HEADERS=ON"
cppFlags "-std=c++11"
}
}
}
buildTypes {
debug {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
ndk {
debuggable true
}
}
release {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
ndk {
debuggable true
}
}
}

externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
}

最佳答案

我查看了崩溃中提到的源位置,对于您的 NDK (r14) 版本:new.cc

从表面上看,new 尝试了 malloc,但失败了,返回了一个 nullptr,然后它尝试获取当前的 new_handler,失败了,因为一个没有设置,所以被强行爆了。

您似乎只是用完了 32 位 Android 设备上的内存。如果您运行的是低于 4.4 的 Android 版本,那么您的应用可能无法分配比物理内存更多的虚拟内存(Android < 4.4 不使用任何类型的交换内存)。

  • 在 4.4 之后,他们添加了 zRAM(页面压缩),这可能会有所帮助:可以压缩脏页以占用更少的 RAM。如果可以,请尝试在运行 4.4+ 的 32 位设备上运行。
  • 或者,在具有更多物理内存的 32 位设备上。

关于c++ - NDK 问题 : crash on 32bit, 在 64 位上不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42891517/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com