boostorg / boostorg/fiber

buffered_channel calls deleter twice when using iterators

Open
#291 4 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
510
Forks
123
PR merge metrics
No merged PRs in 30d

Description

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
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.