分配 std::function 导致段错误

Assigning std::function causes segfault

我有一个奇怪的分段错误,我无法正确理解。我正在用 std::bind 创建一个函数对象,然后将其分配给 std::function 对象,这显然会导致段错误。

std::function<ara::com::Future<SetShapeOutput>(const 
                    messages::test::SetShapeParams&)> SetShape_callback_;

void set_SetShape_callback(const std::function<ara::com::Future<SetShapeOutput>(
        const messages::test::SetShapeParams&)>& callback) {
    SetShape_callback_ = callback;
}

[其他地方]

algo_service_.GetshapeServiceSkeleton()->set_SetShape_callback(
                            std::bind(&ShapeServerAraBinding::on_call_serviceshapeService_methodSetShape,
                                    this, std::placeholders::_1));



// definition
ara::com::Future<adaptiveautosarapplication::shapeServiceSkeleton::SetShapeOutput>
                on_call_serviceshapeService_methodSetShape(
                    const messages::test::SetShapeParams& araSetShapeParams);

来自 gdb 的堆栈跟踪显示分配导致段错误:

#0  0x000055c45c268839 in std::swap<std::_Any_data> (__a=..., __b=...) at /usr/include/c++/6/bits/move.h:191
#1  0x000055c45c267781 in std::function<ara::com::Future<serviceInterfaces::test::shapeService::SetShapeOutput> (messages::test::SetShapeParams const&)>::swap(s
td::function<ara::com::Future<serviceInterfaces::test::shapeService::SetShapeOutput> (messages::test::SetShapeParams const&)>&) (this=0x7fffea5d6be0, __x=...)
    at /usr/include/c++/6/functional:2016
#2  0x000055c45c263934 in std::function<ara::com::Future<serviceInterfaces::test::shapeService::SetShapeOutput> (messages::test::SetShapeParams const&)>::operat
or=(std::function<ara::com::Future<serviceInterfaces::test::shapeService::SetShapeOutput> (messages::test::SetShapeParams const&)> const&) (this=0x58, __x=...)
    at /usr/include/c++/6/functional:1931
#3  0x000055c45c26009f in shapeServer::adaptiveautosarapplication::shapeServiceSkeleton::set_SetShape_callback(std::function<ara::com::Future<serviceInterfaces:
:test::shapeService::SetShapeOutput> (messages::test::SetShapeParams const&)> const&) (this=0x0, callback=...)
    at /app/tests/eclipseProject/projects/shapeRPC/build/autogen/algos/shape/server/ara/include/shapeServer_service.h:40
#4  0x000055c45c260508 in shapeServer::ShapeServerAraBinding::Initialize (this=0x7fffea5d6dd0)
    at /app/tests/eclipseProject/projects/shapeRPC/build/autogen/algos/shape/server/ara/include/shapeServerAraBinding.h:69
#5  0x000055c45c25854c in main (argc=1, argv=0x7fffea5d6fd8)
    at /app/tests/eclipseProject/projects/shapeRPC/build/autogen/algos/shape/server/ara/src/shapeServerAraMain.cpp:108

问题原因如下行所示:

#3  0x000055c45c26009f in shapeServer::adaptiveautosarapplication::shapeServiceSkeleton::set_SetShape_callback(std::function<ara::com::Future<serviceInterfaces:
:test::shapeService::SetShapeOutput> (messages::test::SetShapeParams const&)> const&) (this=0x0, callback=...)

备注shapeServiceSkeletonthis=0x0