簡體   English   中英

dynamic_bitset,讓我的程序崩潰

[英]dynamic_bitset, crash my program

我是新手。 我有一個在 lambda 函數中使用dynamic_bitset的程序。 在我嘗試運行該程序后,我收到了這條消息。 即使沒有初始化位集的bitset和處理它的函數,也會出現此消息。

有誰知道這條消息是什么意思,可能是什么問題?

消息:

/usr/include/boost/dynamic_bitset/dynamic_bitset.hpp:616: boost::dynamic_bitset<Block, Allocator>::~dynamic_bitset() [with Block = long unsigned int, Allocator = std::allocator<long unsigned int>] : 斷言'm_check_invariants()'失敗。 中止

代碼是這樣的
對該函數的主要調用:

int Molecule::initSimilarity(int depth){
    cout << "_size is: " << _size << "\t depth is: " << depth << endl; //TODO delete
    AtomSet viewing(_size);
    int m = 0;
    {
        // break into initial groups by symbol and valancy
        for(int i=0 ; i<_size ; i++)
        {
            if(viewing[i]) continue;
            AtomSet mask = getSetMask( //AtomSet is typedef for dynamic_bitset
                [&](const Atom& b)->bool
                {
                    return (!viewing[b._index] && b._valence == _atoms[i]->_valence && strcmp(b._symbol, _atoms[i]->_symbol) == 0);
                },
                [&](Atom &b)
                {
                    b._class = m; //set the equivalence class of atom 'b' to 'm'
                }
            );
            m++;
            viewing |= mask; //viewing now contains a set of atoms and for each atom it's equivalence class
        }
        cout << "number of equivalence class: " << m << endl; //TODO DELETE!
    }    
    for (int j = 0; j < depth ; j++){
        AtomSet viewed(_size);

        int before = m;
        // iteratively refine the breakdown into groups
        for (int i = 0 ; i < _size ; i++)   //for any atom A
        {
            if (viewed[i]) continue;
            viewed.flip(i);
            AtomSet mask = getSetMask(//put all atoms which are equivalnt but not similar to A in
                //their own equivalence class
                [&](const Atom& b)->bool
                {
                    if (viewed[b._index])
                        return false; //if b is in viewed return false;
                    if (_atoms[i]->_class == b._class) //if in the same class add b to viewed
                    {
                        viewed.flip(b._index);
                        bool similar = !isSimilar(*_atoms[i],b);
                        return similar;
                    }
                    return false;
                },
                [&m](Atom& b)
                {
                    b._class = m;
                }
            );
            if (!mask.none()) m++;
        }
        if (before == m){
            std::cout << "Finished early after just " << j << " iterations" << std::endl;
            return m;
        }
    }
    return m;
}

getSetMask 的簽名是:AtomSet getSetMask(std::function property, std::function action);

最奇怪的是,即使我刪除了該函數的所有內容,它仍然給我錯誤消息

我有一個與 dynamic_bitset 類似的問題,通過在它被破壞之前調用 reset() 解決了它。

您在 lambda 中引用的dynamic_bitset變量可能已經超出 scope 並且已經被破壞,或者類似的東西。 (沒有源代碼很難更具體)

這可能表明您正在寫入超過 bitset 的末尾而不調整它的大小。 可能想做一些邊界檢查。

我遇到了這個問題,我花了 3 個小時才找出問題所在。 以下是可能發生的情況: dynamic_bitset中的operator[]不進行邊界檢查。 因此,可以在允許的范圍之外分配一個值,這不會產生任何錯誤(sanitizer/valgrind 看不到任何東西),因為 dynamic_bitset 使用 64 位整數(至少在我的計算機上)來存儲值。 因此,您可以獲得 32 個存儲的 integer,而在 dynamic_bitset 中只允許 4 位。 該錯誤會在稍后調用m_check_invariant()時觸發,例如在調用析構函數時。

所以,問題就變成了找到這個范圍錯誤。 解決方法是編輯boost/dynamic_bitset.hpp並在調用超出范圍的操作時在operator[]的代碼中添加 print 語句。 如果您不能這樣做,請下載 boost 庫並將其安裝在您的主目錄中。

閱讀 Mathieu Dutour Sikiric 的解釋。 問題是您通過 operator[] 寫入了 bitset 的允許范圍之外,這不會產生任何錯誤,因為它是提升並且不會浪費計算時間來檢查您是否有權寫入您想要的位置。 你知道這是C++...

因此,要檢測它,請轉到boost/dynamic_bitset/dynamic_bitset.hpp ,並修改代碼以在每次使用 operator[] 時都進行檢查。

boost/dynamic_bitset/dynamic_bitset.hpp ,第 300 行左右。

    reference operator[](size_type pos) {
        assert(m_check_invariants());
        return reference(m_bits[block_index(pos)], bit_index(pos));
    }
    bool operator[](size_type pos) const { 
        assert(m_check_invariants());
        return test(pos); 
    }

這使得檢測代碼中的錯誤變得更加容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM