boostorg / boostorg/fiber

buffered_channel calls deleter twice when using iterators

Abierto
#291 4 comentarios 1 reacción 0 asignados Ver en GitHub
Lenguaje dominante
C++
Estrellas
510
Forks
123
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

version: 1.75.0 (probably 1.78.0)
compiler: gcc 9.4.0

Hi, I have found some unexpected behavior when using ```buffered_channel::iterator```. The destructor is called twice when using them.

This happens first time when ```operator++``` is called and second time in ```increment()```
https://github.com/boostorg/fiber/blob/8d0fe6055ad2c2cd98a022ad663d2bbd42e056c9/include/boost/fiber/buffered_channel.hpp#L357-L361
https://github.com/boostorg/fiber/blob/8d0fe6055ad2c2cd98a022ad663d2bbd42e056c9/include/boost/fiber/buffered_channel.hpp#L310-L320

Got this on version 1.75.0 but it seems that it is still present.

Minimal reproducible example:
```cpp
#include
#include

#include

class Foo
{
public:
Foo(int i) : i_(i) {std::cout << "Foo()\n";}
~Foo() {std::cout << "~Foo()\n";}
void PrintI() {std::cout << "Foo::operator()(): " << i_ << "\n";}

private:
int i_ = 0;
};
using FooSptr = std::shared_ptr;

int main(int, char**)
{
boost::fibers::buffered_channel chan(16);

boost::fibers::fiber f([&]() {
std::vector fooVec; // keep objects for some time
for(auto& foo : chan)
{
fooVec.push_back(foo);
}

for (auto& foo : fooVec)
{
foo->PrintI();
}
fooVec.clear();
std::cout << "Fiber finished\n";
});

chan.push(std::make_shared(0));
chan.push(std::make_shared(1));

chan.close();
std::cout << "chan.close();\n";

f.join();

return 0;
}
```

In this example I pass some shared_ptr's through the channel and put it in an array to extend it's lifetime. But counter decrements twice and objects gets destroyed.

Expected result:
```
Foo()
Foo()
chan.close();
Foo::operator()(): 0
Foo::operator()(): 1
~Foo()
~Foo()
Fiber finished
```

Actual result:
```
Foo()
Foo()
chan.close();
~Foo()
~Foo()
Foo::operator()(): 32
Foo::operator()(): 1953066354
Fiber finished
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.