GCC 地址清理程序错误或无效移动?

问题描述 投票:0回答:1

我有一个

boost::asio::io_context
,我想移动它。由于没有指针就无法移动它,因此我使用
std::unique_ptr
。但是,我注意到,当使用
-fsanitize=address
heap-use-after-free 运行时。

我检查了移动前后指向的地址,是完全一样的。 我是否犯了一个错误,或者消毒剂只是在这里反应过度?

#include <boost/asio.hpp>
#include <memory>
#include <iostream>

int main()
{
    std::unique_ptr<boost::asio::io_context> io_ptr{
        std::make_unique<boost::asio::io_context>()
    };

    boost::asio::steady_timer timer{*io_ptr, std::chrono::seconds{1}};
    std::cout << "io_ptr address: " << io_ptr.get() << std::endl;
// on my machine: io_ptr address: 0x602000000010

    timer.async_wait([](const boost::system::error_code&) {
        std::cout << "Timer called\n"; // gets called
    });

    std::unique_ptr<boost::asio::io_context> io_ptr2{std::move(io_ptr)};
    std::cout << "io_ptr address: " << io_ptr2.get() << std::endl;
// same: io_ptr address: 0x602000000010
    io_ptr2->run_one();
    std::cout << "Done\n"; // gets printed

    return 0;
}

我尝试在

async_wait
调用中打印一些内容,并且该内容被打印出来,因此消毒剂似乎对计时器的破坏做出了反应。

编译器:GCC 11.4.0

c++ gcc boost-asio move unique-ptr
1个回答
2
投票

请记住,销毁以相反的顺序发生——这意味着拥有

std::unique_ptr
的第二个
io_context
在计时器对象之前被销毁....

© www.soinside.com 2019 - 2024. All rights reserved.