lundi 26 août 2019

Custom allocator with declared move operator leads to a segfault when used with gcc stl library

Hello when using a custom allocator with a defined move operator, I am getting a segfault (with gcc but not with visual studio).

After having a look at the stl code, It appears that for some containers like deque, the stl move the allocator prior to the data tree destruction and leave the movable input to an "allocated state", leading to a segfault.

_Deque_base(_Deque_base&& __x, true_type)
      : _M_impl(std::move(__x._M_get_Tp_allocator()))
      {
    _M_initialize_map(0);
    if (__x._M_impl._M_map)
      this->_M_impl._M_swap_data(__x._M_impl);
      }

The problem is that the intitialisation of the datas on _M_initialize_map(0) allocate some objects (root node) and as the allocator is moved, on the destruction of the input object, the deallocation of this newly created node (swapped into the old object), is made with an invalid allocator as it has been moved previously.

So my question is, is it a bug of the stl ? Or should a move operator of an allocator behave as a copy operator and not a "move" one ?

Aucun commentaire:

Enregistrer un commentaire