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 
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:
.
Find the GDB manual and other documentation resources online at:
    .

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, std::allocator >::_M_assign(std::__cxx11::basic_string, std::allocator > const&) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007fd08d067b8c in ros::Publisher::Publisher(std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, bool, ros::NodeHandle const&, boost::shared_ptr 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 > > (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 , argc=1, argv=0x7ffcaebf0b68, init=, fini=, rtld_fini=, 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 异常!" <

//
// 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 异常!" <
//
// 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,SIGSEGV,Segmentation,malloc.c,没有那个文件或目录,ros)