boostorg / boostorg/fiber

buffered_channel calls deleter twice when using iterators

未关闭
#291 4 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
C++
星标
510
派生
123
PR 合并指标
30 天内没有已合并 PR

描述

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

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。