STL vector crash on Android x86_64

338 views
Skip to first unread message

hejia...@gmail.com

unread,
Jun 12, 2016, 12:50:03 AM6/12/16
to android-ndk

The following code will crash at vector desconcrutor if I build the code for Android x86_64 with ndk-build(x86 worked well).

hello.cpp

#include <vector>
#include <stdio.h>

using namespace std;

int main(int argc, char* argv[])
{
    vector<int> vec;

    vec.push_back(11);
    vec.clear();

    vector<int> vec2(vec.size());

    return 0;    
}

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello
LOCAL_SRC_FILES := hello.cpp

include $(BUILD_EXECUTABLE)

Application.mk

APP_ABI := x86_64

APP_STL := stlport_static

enter image description here

It is weird. The value of vec.size() equals to 0. If I change the code as follows:

vector<int> vec2(0);

the app will not crash. The issue seems to be caused by compiler optimization. CFLAG -O2 is used by default. If I add another CFLAG, such as -O0 after it, the app will not crash either.

Does anybody know why?

Thanks!

Dan Albert

unread,
Jun 15, 2016, 2:29:21 PM6/15/16
to android-ndk
You're using r10e, which has a much older and much worse version of both compilers. You should update to r12.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/80a8af34-84b4-4d27-9418-cb92b358d6cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

hejia...@gmail.com

unread,
Jul 4, 2016, 12:34:36 PM7/4/16
to android-ndk
I tested r12, the same error.

Dan Albert

unread,
Jul 6, 2016, 4:56:53 PM7/6/16
to android-ndk

Tiger Hwang

unread,
Aug 27, 2016, 11:00:40 AM8/27/16
to android-ndk
I encounter the same problem, I think there must be a bug in std::vector implementation.
I try to push 10 elements to vector, it crashed at 4rd element.  The element is a template class to hold some data, like auto_ptr in boost.
The same algorithm works in windows and iOS.
Then I use std::map to do it, using size of map as key, no more crash.

Reply all
Reply to author
Forward
0 new messages