【问题标题】:Assigning std::function causes segfault分配 std::function 会导致段错误
【发布时间】:2019-08-22 17:21:05
【问题描述】:

我有一个奇怪的分段错误,我无法正确理解。我正在使用 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

【问题讨论】:

  • 我遇到了同样的问题,因为 freference。您是否尝试过按值传递callback?然而,您将reference 分配给其他东西。这可能不是这里的问题。您也可能会遇到悬空函数参数的问题。
  • this=0x0 建议 GetshapeServiceSkeleton() 返回一个空指针。请提供minimal reproducible example
  • 不相关,但为什么不使用 lambdas 而不是 std::bind

标签: c++ std-function


【解决方案1】:

问题原因如下一行所示:

#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

【讨论】:

  • 谢谢,这很有价值,因为这是我将来可以寻找的东西。我其实没有初始化shapeServiceSkeleton
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多