hash_table STL C ++崩溃

时间:2018-03-22 03:10:10

标签: android c++ android-ndk hashmap

我在JNI的Android应用程序中开发库c ++。 我使用std :: unordered_map和std :: unordered_set来存储和访问数据。 我有一个mData,它是黑名单数据。然后,当我在黑名单数据中添加新单词时,它会使应用程序崩溃并在结构中推送崩溃信息。

应用程序崩溃某些时间和某些设备。我无法回避问题。太伤心了。

struct CodePoints {
    explicit CodePoints() { }

    explicit CodePoints(const int * codePoints, const int codePointsCount) {
        mCodePoints.insert(mCodePoints.end(), codePoints, codePoints + codePointsCount);
    }

    explicit CodePoints(const std::vector<int>& codePoints) {
        mCodePoints.insert(mCodePoints.end(), codePoints.begin(), codePoints.end());
    }

    CodePoints(const CodePoints & rhs) {
        mCodePoints.insert(mCodePoints.end(), rhs.mCodePoints.begin(), rhs.mCodePoints.end());
    }

    virtual ~CodePoints() {}

    CodePoints & operator=(const CodePoints& rhs) {
        if (this != &rhs) {
            mCodePoints.assign(rhs.mCodePoints.begin(), rhs.mCodePoints.end());
        }
        return *this;
    }
    bool operator==(const CodePoints& rhs) const {
        if (codePointsCount() != rhs.codePointsCount()) {
            return false;
        }
        int size = codePointsCount();
        for (int i = 0; i < size; ++ i) {
            if ( this->get(i) != rhs.get(i) ) {
                return false;
            }
        }
        return true;
    }
    friend struct std::hash<CodePoints>;
    template< class InputIt, class UnaryFunction > friend
    UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f );

    std::vector<int> mCodePoints;
};

#define MAX_SIZE_BLACKLIST_DICT 20000
typedef std::unordered_set<CodePoints> BlacklistType;

    BlacklistType mData;
    int mSize;

    void BlacklistMapper::addWord(CodePoints word) {
        if(mSize >= MAX_SIZE_BLACKLIST_DICT) {
            AKLOGE("Max size Blacklist. Can not add new word into blacklist");
            return;
        };
        mData.insert(word);
        mSize++;
    }
 struct hash<CodePoints>
{
    size_t operator()(const CodePoints& codePoints) const
    {
        size_t hashCode{0};
        for_each(codePoints.mCodePoints.begin(), codePoints.mCodePoints.end(), [&hashCode](int codePoint){ hashCode = 31 * hashCode + codePoint; });
        return hashCode;
    }
};

这是结构中的崩溃信息

0  libjni_labanime_v1_4_1.so      0xca9e4ffa latinime::BlacklistMapper::addWord(latinime::CodePoints) (__hash_table:102)
1  (Missing)                      0xecd656df (Missing)
2  libjni_labanime_v1_4_1.so      0xca9e0b19 latinime::latinime_BlacklistMapper_addWord(_JNIEnv*, _jclass*, _jintArray*) (com_android_inputmethod_latin_BlacklistMapper.cpp:29)
3  libjni_labanime_v1_4_1.so      0xca9e0a99 latinime::latinime_BlacklistMapper_addWord(_JNIEnv*, _jclass*, _jintArray*) (jni.h:854)
4  (Missing)                      0xecd656df (Missing)

谢谢。

0 个答案:

没有答案