ros中 指针传递 ros::NodeHandle *node 报received signal SIGSEGV, Segmentation fault

ros中 指针传递 ros::NodeHandle *node

报错:

 Thread 1 "currencyChassis" received signal SIGSEGV, Segmentation fault. 0x00007fa8503fa3a6 in __GI___libc_realloc (oldmem=0x55ccc9afef20, bytes=64) at malloc.c:3233

   GNU gdb (GDB; JetBrains IDE bundle; build 126) 8.2
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
No source file named /home/iimt/workspace/workspace_ros_foodcar_noetic/src/slamservice/slamservice/src/currencyChassis/currencyChassis_savePoseByOdom_sql.cpp.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fd0881d2700 (LWP 798524)]
[New Thread 0x7fd0879d1700 (LWP 798525)]
[New Thread 0x7fd0871d0700 (LWP 798526)]
[New Thread 0x7fd0869cf700 (LWP 798527)]
[New Thread 0x7fd077fff700 (LWP 798528)]
[New Thread 0x7fd076bb8700 (LWP 798529)]
[New Thread 0x7fd0763b7700 (LWP 798530)]
[New Thread 0x7fd075905700 (LWP 798538)]

Thread 1 "currencyChassis" received signal SIGSEGV, Segmentation fault.
0x00007fd08bc10464 in __GI___libc_malloc (bytes=33) at malloc.c:3079
3079	malloc.c: 没有那个文件或目录.
(gdb) bt
#0  0x00007fd08bc10464 in __GI___libc_malloc (bytes=33) at malloc.c:3079
#1  0x00007fd08bf79b39 in operator new(unsigned long) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007fd08c01229f in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007fd08d067b8c in ros::Publisher::Publisher(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, ros::NodeHandle const&, boost::shared_ptr<ros::SubscriberCallbacks> const&) () from /opt/ros/noetic/lib/libroscpp.so
#4  0x00007fd08d0a0e24 in ros::NodeHandle::advertise(ros::AdvertiseOptions&) () from /opt/ros/noetic/lib/libroscpp.so
#5  0x00005582511d85fb in ros::NodeHandle::advertise<disinfect_msg::startCar_msg_<std::allocator<void> > > (this=0x7ffcaebf09a0, topic="/start_car", queue_size=10, latch=false) at /opt/ros/noetic/include/ros/node_handle.h:254
#6  0x00005582511e7758 in SocketData::initRosCommunication (this=0x558252b9ca00) at /home/iimt/workspace/workspace_ros_foodcar_noetic/src/slamservice/slamservice/src/NetWork/SocketData.cpp:70
#7  0x00005582511e72a0 in SocketData::SocketData (this=0x558252b9ca00, node=0x7ffcaebf09a0, parent=0x0) at /home/iimt/workspace/workspace_ros_foodcar_noetic/src/slamservice/slamservice/src/NetWork/SocketData.cpp:24
#8  0x00005582511d5549 in SocketData::getInstance (node=0x7ffcaebf09a0) at /home/iimt/workspace/workspace_ros_foodcar_noetic/src/slamservice/slamservice/src/NetWork/SocketData.h:237
#9  0x00005582511d096c in NetworkServer::NetworkServer (this=0x7ffcaebf0830, node=0x7ffcaebf09a0, parent=0x0) at /home/iimt/workspace/workspace_ros_foodcar_noetic/src/slamservice/slamservice/src/NetWork/NetworkServer.cpp:19
#10 0x00005582511d00a3 in main (argc=1, argv=0x7ffcaebf0b68) at /home/iimt/workspace/workspace_ros_foodcar_noetic/src/slamservice/slamservice/src/NetWork/main.cpp:34
#11 0x00007fd08bb9a0b3 in __libc_start_main (main=0x5582511cff48 <main(int, char**)>, argc=1, argv=0x7ffcaebf0b68, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffcaebf0b58) at ../csu/libc-start.c:308
#12 0x00005582511ca60e in _start ()
Attempt to use a type name as an expression.
Attempt to use a type name as an expression.
[Switching to thread 2 (Thread 0x7fd0881d2700 (LWP 798524))]
Couldn't get registers: 没有那个进程.
[Thread 0x7fd075905700 (LWP 798538) exited]
[Thread 0x7fd0763b7700 (LWP 798530) exited]
[Thread 0x7fd076bb8700 (LWP 798529) exited]
[Thread 0x7fd077fff700 (LWP 798528) exited]
[Thread 0x7fd0869cf700 (LWP 798527) exited]
[Thread 0x7fd0871d0700 (LWP 798526) exited]
[Thread 0x7fd0879d1700 (LWP 798525) exited]
[Thread 0x7fd0881d2700 (LWP 798524) exited]

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.

应该是:指针传递错误

解决办法:

       把指针传递改为引用传递;

ros 代码 ( ros::NodeHandle *node 指针传递)


int main(int argc, char *argv[]) {
    //节点名
    string nodeName = "currencyChassis_NetWorkNode";
    //初始化节点
    ros::init(argc,argv,nodeName);
    //创建节点
    ros::NodeHandle node;

    QApplication app(argc, argv);
    try {
    //数据解析类
    SocketData::getInstance(&node);

  
    } catch (std::exception e) {
        cout << "main currencyChassis_NetWorkNode 异常!" <<e.what()<< endl;
    }


    return QApplication::exec();
}

//
// Created by iimt on 21-12-1.
//

#ifndef SLAMSERVICE_SOCKETDATA_H
#define SLAMSERVICE_SOCKETDATA_H

using namespace std;
class SocketData:  public QWidget {
    Q_OBJECT
private:
    //私有三个构造函数
    SocketData(ros::NodeHandle *node, QWidget *parent = 0);
    SocketData(const  SocketData&);
    SocketData operator=(const  SocketData&);
    static mutex *m ;
    ros::NodeHandle *node;
public:
    ~SocketData();
            static SocketData * getInstance(ros::NodeHandle *node){
                if(instance == nullptr){
                    m->lock();
                    //懒汉式
                    if(instance == nullptr){
                        instance = new SocketData(node);
                    }
                    m->unlock();
                }
                return instance;
            }
};


#endif //SLAMSERVICE_SOCKETDATA_H

改 (ros::NodeHandle &node )引用传递后,代码不报错


int main(int argc, char *argv[]) {
    //节点名
    string nodeName = "currencyChassis_NetWorkNode";
    //初始化节点
    ros::init(argc,argv,nodeName);
    //创建节点
    ros::NodeHandle node;

    QApplication app(argc, argv);
    try {
    //数据解析类
    SocketData::getInstance(node);

  
    } catch (std::exception e) {
        cout << "main currencyChassis_NetWorkNode 异常!" <<e.what()<< endl;
    }


    return QApplication::exec();
}
//
// Created by iimt on 21-12-1.
//

#ifndef SLAMSERVICE_SOCKETDATA_H
#define SLAMSERVICE_SOCKETDATA_H

using namespace std;
class SocketData:  public QWidget {
    Q_OBJECT
private:
    //私有三个构造函数
    SocketData(ros::NodeHandle &node, QWidget *parent = 0);
    SocketData(const  SocketData&);
    SocketData operator=(const  SocketData&);
    static mutex *m ;
    ros::NodeHandle node;
public:
    ~SocketData();
            static SocketData * getInstance(ros::NodeHandle &node){
                if(instance == nullptr){
                    m->lock();
                    //懒汉式
                    if(instance == nullptr){
                        instance = new SocketData(node);
                    }
                    m->unlock();
                }
                return instance;
            }
};


#endif //SLAMSERVICE_SOCKETDATA_H

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_无往而不胜_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值