'Segmentation Fault when i use std::packaged_task?

these code complie success but running error. like

#include <thread>
#include <future>
#include <functional>
#include <memory>

int func(int x, int y)
{
    return x + y;
}

void main()
{
    std::packaged_task<int()> task(std::bind(func, 2, 11));
    std::future<int> ret = task.get_future();
    task();
    std::cout << ret.get() << std::endl;
}

segment infomation :

#1  0x56cdb624 in std::__future_base::_State_baseV2::_M_set_result(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>, bool) (this=0x578a814c, __res=..., __ignore_failure=false) at /opt/buildtools/gcc-7.3.0/include/c++/7.3.0/future:401
#2  0x56cde1bc in std::__future_base::_Task_state<std::_Bind<int (*(int, int))(int, int)>, std::allocator<int>, int ()>::_M_run() (this=0x578a814c)
    at /opt/buildtools/gcc-7.3.0/include/c++/7.3.0/future:1423
#3  0x56cdc239 in std::packaged_task<int ()>::operator()() (this=0xffffc154) at /opt/buildtools/gcc-7.3.0/include/c++/7.3.0/future:1556
#4  0x56cdb072 in test () at /home/builduser/code/sourcecode/otn_xcs_board/bd_otn_test/code/test_frame/test_main.cpp:22
#5  0x56cdb11d in main (argc=1, argv=0xffffc244) at /home/builduser/code/sourcecode/otn_xcs_board/bd_otn_test/code/test_frame/test_main.cpp:28

i try to run same code in another environment, visual studio 2022 or clion . everything goes well.

but same code in linux environment gets segment fault, why ?

version g++ (GCC) 7.3.0

c++17



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source