Search code examples
c++std-function

Assigning std::function causes segfault


I have a strange segmentation fault I am unable to properly understand. I am creating a function object with std::bind, then assign this to a std::function object, this apparently results in a segfault.

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;
}

[somewhere else]

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);

And the stacktrace from gdb showing the assignment causes a segfault:

#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

Solution

  • The problem cause is shown in the following line:

    #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=...)
    

    Note shapeServiceSkeleton this=0x0.