简体   繁体   中英

dynamic_bitset, crash my program

I'm new with boost. I have a program which uses dynamic_bitset inside a lambda function. After I try to run the program, I get this message. This message appears even without the function that initializes the bitset and the functions that handle it.

Does anybody know what this message means and what might be the problem?

The message:

/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>] : Assertion 'm_check_invariants()' failed. Aborted

well the code is something like this
main call to this function:

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

the signature of getSetMask is: AtomSet getSetMask(std::function property, std::function action);

and the weirdest thing that even when i remove all the content of that function it still give me the error message

I had a similar problem with dynamic_bitset that was solved by calling reset() on it before it got destroyed.

Probably the dynamic_bitset variable that you are referencing in the lambda has gone out of scope and has already been destroyed, or something similar. (Without the source code it's difficult to be more specific)

That can indicate that you are writing past the end of the bitset without resizing it. Might want to do some bounds checking.

I had that problem and it took me 3 hours to find out the problem. Here is what can happen: The operator[] in dynamic_bitset does not do bound checking. So, one value can be assigned outside of allowed range and this does not create any error (sanitizer/valgrind do not see anything) since dynamic_bitset is using 64 bit integers (on my computer at least) in order to store values. So, you can get a stored integer of 32 while you allowed only 4 bits in the dynamic_bitset. The error is triggered at a later time when m_check_invariant() is called for example when the destructor is called.

So, the problem becomes to find this range error. The solution is to edit the boost/dynamic_bitset.hpp and add print statement in the code of operator[] when an operation out of range is called. If you cannot do that then download the boost library and install it in your home directory.

Read the explaination of Mathieu Dutour Sikiric. The problem is that you write outside of allowed range of the bitset via operator[] and this does not create any error because it's boost and it doesn't bother to waste compute time checking that you have right to write where you want. It is C++ you know...

So to detect it, go to boost/dynamic_bitset/dynamic_bitset.hpp , and modify the code to impose checks every time you use operator[].

boost/dynamic_bitset/dynamic_bitset.hpp , around line 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); 
    }

This makes it easier to detect the error in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM