Need help with Segment Fault in dynamic_cast

21 views
Skip to first unread message

陈依伦

unread,
Dec 18, 2020, 2:16:46 PM12/18/20
to OMNeT++ Users
I was doing a demo of Omnet++.
I met this Segment Fault and had no idea about it.
Could anyone help me ?
[code]
  #include "include\sdn.h"
  #include <stdlib.h>

  #include <stdio.h>
  #include <unordered_map>
  #include <vector>
  #include <omnetpp.h>
  #include <omnetpp\cqueue.h>

  #include "omnetpp/cqueue.h"
  #include "RegistryPacket_m.h"
  #include "Datagram_m.h"
  #include "RoutingData_m.h"
  #include "Timer_m.h"
  #include "HelloAnswer_m.h"

using namespace omnetpp;
using std::pair;
using std::shared_ptr;
using std::unordered_map;
using std::vector;

class LEO_EX : public cSimpleModule
{
private:
    unordered_map<int, bool> neighbors;
    cQueue queue;
    int routingTable[LINES * COLUMNS];
    int i,lines,columns,dest,faultModuleNum,ISL,maxPktLength,minPktLength;
    double faultTime,hello_timeout,valid_time,restore_time,processing_time,l2l_propagationDelay,l2l_duration,l2g_propagationDelay,l2g_duration,totalBits,time;
    int64_t pktLength;
    long numSent,numReceived;

protected:
    cFSM fsm;
    cOutVector throughputVec;
    cOutVector endToEndDelayVec;

    enum
    {
        INIT = 0,
        CONNECTING = FSM_Steady(1),
        REGISTER = FSM_Steady(2),
        ONLINE = FSM_Steady(3),
        OFFLINE = FSM_Steady(4),
    };
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
    virtual cMessage *generateHelloMessage();
    RegistryPacket *generateNeighborMessage(int mode);
    virtual void getNeighbors();
    virtual void registerInGEO();
    void sendNeighborInfo(int mode);
    virtual RoutingData *generateRoutingRequest(Datagram *msg);
    virtual void forwardDatagram(Datagram *msg, int portNum);
    virtual void initialRoutingTable();
    virtual void sendRoutingRequest(Datagram *msg);
    virtual int getNextHop(int destAddr);
    virtual void setRouting(int destAddr, int srcAddr);
    virtual void sendDatagram();
    virtual Datagram *generateDatagram();
    void setTimer(double timeout, int kind, int moduleId = 100);
    void destroy();
    void sayHello(cModule *module);
    void probeNeighbors();
    void answerHello(cModule *source);
    void enableConnectionTo(int id);
    HelloAnswer *generateHelloAnswer(double valid_time);
    void disableConnectionTo(int id);
    void refreshDisplay() const override;
    void sendLater(cMessage *msg);

};
Define_Module(LEO_EX);

void LEO_EX::initialize()
{
    
    
    queue = cQueue("queue");
    //...
    getNeighbors();
    probeNeighbors();
    initialRoutingTable();
    EV << "Registering in GEO..." << endl;

}

void LEO_EX::handleMessage(cMessage *msg)
{
    Datagram *data = nullptr;
    FSM_Switch(fsm)
    {
    case FSM_Exit(INIT):
        setTimer(faultTime, FAULT);
        FSM_Goto(fsm, CONNECTING);
        break;
    case FSM_Enter(CONNECTING):
    {
        int b = msg->getKind();
        EV << "type:" << b << endl;
        switch (b)
        {
        case HELLO:
        {
            cModule *source = msg->getSenderModule();
            int id = source->getIndex();
            enableConnectionTo(id);
            setRouting(id, id);
            ISL++;
            answerHello(source);
            break;
        }
        case HELLO_TIMEOUT:
            ISL++;
            break;
        case HELLO_ANSWER:
        {
            HelloAnswer *answer;
            answer = check_and_cast<HelloAnswer *>(msg);
            double repeatTime = answer->getValidTime();
            setTimer(repeatTime, CONNECTING_TIME, answer->getSenderModule()->getIndex());
            break;
        }
        }
        delete msg;
        break;
    }
    case FSM_Exit(CONNECTING):
    {
        int j = 0;
        for (auto &x : neighbors)
        {
            if (x.second)
            {
                j++;
            }
        }
        if (j == (int)neighbors.size())
        {
            registerInGEO();
            FSM_Goto(fsm, REGISTER);
        }
        break;
    }
    case FSM_Enter(REGISTER):
        if (msg->getKind() == NEIGHBOR_QUERY)
        {
            sendNeighborInfo(INIT_NEIGHBOR);
            sendDatagram();
        }
        else if (msg->getKind() == HELLO_ANSWER)
        {
            HelloAnswer *answer;
            answer = check_and_cast<HelloAnswer *>(msg);
            double repeatTime = answer->getValidTime();
            setTimer(repeatTime, CONNECTING_TIME, answer->getSenderModule()->getIndex());
            delete answer;
        }
        break;
    case FSM_Exit(REGISTER):
        FSM_Goto(fsm, ONLINE);
        break;
    case FSM_Enter(ONLINE):
    {
        int kind = msg->getKind();
        if (kind == DATAGRAM)
        {
            EV << getFullName() << ":Received Packet " << msg << endl;
            break;
        }
        else if (kind == ROUTE_ANSWER)
        {
            RoutingData *routingData = check_and_cast<RoutingData *>(msg);
            data = routingData->getData();
            if (routingData->getNextHop() == NOT_FOUND)
            {
                sendLater(data);
                data = nullptr;
            }
            else
            {
                setRouting(routingData->getDestAddr(), routingData->getNextHop());
            }
            break;
        }
        else if (kind == PROCESSED)
        {
            EV << "Replying!\n";
            sendDatagram();
            break;
        }
        else if (kind == CONNECTION_EXPIRED)
        {
            Timer *timer = check_and_cast<Timer *>(msg);
            int id = timer->getModuleId();
            disableConnectionTo(id);
            setRouting(id, UNKNOWN);
            sendNeighborInfo(UPDATE_NEIGHBOR);
            cancelAndDelete(msg);
            break;
        }
        else if (kind == HELLO_ANSWER)
        {
            HelloAnswer *answer;
            answer = check_and_cast<HelloAnswer *>(msg);
            double repeatTime = answer->getValidTime();
            setTimer(repeatTime, CONNECTING_TIME, answer->getSenderModule()->getIndex());
            delete msg;
            break;
        }

        if (data != nullptr)
        {
            int dest = data->getDestAddr();
            if (dest == getIndex()) 
            {
                //...
            }
            else 
            {
                //...
            }
            data = nullptr;
        }
        break;
    }
    case FSM_Exit(ONLINE):
        if (faultTime - simTime().dbl() < 0.0001)
        {
            if (faultModuleNum == getIndex())
            {
                EV << getFullName() << ":No." << faultModuleNum << " is time to destroy!" << endl;
                setTimer(restore_time, NORMAL);
                FSM_Goto(fsm, OFFLINE);
                EV << "Broken!\n";
            }
        }
        else if (msg->getKind() == CONNECTING_TIME)
        {
            Timer *timer = check_and_cast<Timer *>(msg);
            cModule *network = getParentModule();
            int id = timer->getModuleId();
            cModule *neighbor = network->getSubmodule("leo", id);
            EV << "Saying hello to " << neighbor << endl;
            ISL--;
            FSM_Goto(fsm, ONLINE);
            sayHello(neighbor);
            cancelAndDelete(msg);
        }
        break;
    case FSM_Enter(OFFLINE):
    {
        EV << "Drop message simply!\n";
       
        queue.insert(msg);

        EV << "Store message in queue simply!\n";
        break;
    }
    case FSM_Exit(OFFLINE):
        if (msg->getKind() == NORMAL)
        {
            if (faultModuleNum == getIndex())
            {
                EV << getFullName() << ":No." << faultModuleNum << " is time to resume!" << endl;
                FSM_Goto(fsm, ONLINE);
                while (!queue.isEmpty())
                {
                    cMessage *store = (cMessage *)queue.pop();
                    sendDirect(store, this, "radioIn");
                }
                EV << "Recovering!\n";
            }
        }
        cancelAndDelete(msg);
        break;
    }
}
...
[/code]

And below is GDB backtrace:

New UI allocated
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
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-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".
(gdb) Traceback (most recent call last):
  File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25-gdb.py", line 60, in <module>
    from libstdcxx.v6 import register_libstdcxx_printers
ImportError: cannot import name 'register_libstdcxx_printers'
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffe6f83700 (LWP 7868)]
[New Thread 0x7fffe5b0f700 (LWP 7869)]
[New Thread 0x7fffdd29e700 (LWP 7872)]
[New Thread 0x7fffdca9d700 (LWP 7873)]
[New Thread 0x7fffd390d700 (LWP 7874)]
[New Thread 0x7fffd310c700 (LWP 7875)]
[New Thread 0x7fffd290b700 (LWP 7876)]
[New Thread 0x7fffd210a700 (LWP 7877)]
[New Thread 0x7fffd1909700 (LWP 7878)]
[New Thread 0x7fffd1108700 (LWP 7879)]

Thread 1 "sdn_satellite_d" received signal SIGSEGV, Segmentation fault.
0x00007ffff6330410 in __dynamic_cast () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
bt
#0  0x00007ffff6330410 in __dynamic_cast () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x000055555557a4be in omnetpp::check_and_cast<RoutingData*, omnetpp::cMessage> (p=0x5555567c84c0) at /home/omnetpp-5.6.1/include/omnetpp/checkandcast.h:65
#2  0x00005555555866aa in LEO_EX::handleMessage (this=0x555556493150, msg=0x5555567c84c0) at LEO_EX.cc:277
#3  0x00007ffff6b432d4 in omnetpp::cSimulation::doMessageEvent (this=0x55555583fab0, msg=0x5555567c84c0, module=0x555556493150) at csimulation.cc:671
#4  0x00007ffff6b42f15 in omnetpp::cSimulation::executeEvent (this=0x55555583fab0, event=0x5555567c84c0) at csimulation.cc:613
#5  0x00007ffff7486ac9 in omnetpp::qtenv::Qtenv::doRunSimulation (this=0x555555834a50) at qtenv.cc:894
#6  0x00007ffff7486399 in omnetpp::qtenv::Qtenv::runSimulation (this=0x555555834a50, mode=omnetpp::qtenv::RUNMODE_NORMAL, until_time=..., until_eventnum=0, until_msg=0x0, until_module=0x0, stopOnMsgCancel=true) at qtenv.cc:744
#7  0x00007ffff73faf4d in omnetpp::qtenv::MainWindow::runSimulation (this=0x555556149bc0, runMode=omnetpp::qtenv::RUNMODE_NORMAL) at mainwindow.cc:500
#8  0x00007ffff7511b89 in omnetpp::qtenv::MainWindow::on_actionRun_triggered (this=0x555556149bc0) at mainwindow.h:97
#9  0x00007ffff7511427 in omnetpp::qtenv::MainWindow::qt_static_metacall (_o=0x555556149bc0, _c=QMetaObject::InvokeMetaMethod, _id=3, _a=0x7fffffffc960) at moc_mainwindow.cpp:297
#10 0x00007ffff7511a42 in omnetpp::qtenv::MainWindow::qt_metacall (this=0x555556149bc0, _c=QMetaObject::InvokeMetaMethod, _id=3, _a=0x7fffffffc960) at moc_mainwindow.cpp:396
#11 0x00007ffff4585619 in QMetaObject::activate(QObject*, int, int, void**) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#12 0x00007ffff3bd3122 in QAction::triggered(bool) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#13 0x00007ffff3bd580c in QAction::activate(QAction::ActionEvent) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#14 0x00007ffff3ccc12b in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#15 0x00007ffff3ccc38d in QAbstractButton::mouseReleaseEvent(QMouseEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#16 0x00007ffff3dab01a in QToolButton::mouseReleaseEvent(QMouseEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#17 0x00007ffff3c18048 in QWidget::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#18 0x00007ffff3dab0b4 in QToolButton::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#19 0x00007ffff3bd983c in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#20 0x00007ffff3be165f in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#21 0x00007ffff45568d8 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#22 0x00007ffff3be0632 in QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer<QWidget>&, bool) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#23 0x00007ffff3c3316b in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#24 0x00007ffff3c357da in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#25 0x00007ffff3bd983c in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from/usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#26 0x00007ffff3be1104 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#27 0x00007ffff45568d8 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#28 0x00007ffff4b18583 in QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5
#29 0x00007ffff4b1a055 in QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5
#30 0x00007ffff4af12eb in QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5
#31 0x00007fffeb682260 in ?? () from /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5
#32 0x00007ffff167c417 in g_main_context_dispatch () from /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0
#33 0x00007ffff167c650 in ?? () from /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0
#34 0x00007ffff167c6dc in g_main_context_iteration () from /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0
#35 0x00007ffff45af88f in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#36 0x00007ffff455490a in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#37 0x00007ffff455d9b4 in QCoreApplication::exec() () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#38 0x00007ffff7485a16 in omnetpp::qtenv::Qtenv::doRun (this=0x555555834a50) at qtenv.cc:632
#39 0x00007ffff78e890e in omnetpp::envir::EnvirBase::run (this=0x555555834a60) at envirbase.cc:747
#40 0x00007ffff78e6232 in omnetpp::envir::EnvirBase::run (this=0x555555834a60, argc=4, argv=0x7fffffffdbe8, configobject=0x5555557d2ac0) at envirbase.cc:358
#41 0x00007ffff78ddf40 in omnetpp::envir::setupUserInterface (argc=4, argv=0x7fffffffdbe8) at startup.cc:259
#42 0x00007ffff78df3e9 in omnetpp::envir::evMain (argc=4, argv=0x7fffffffdbe8) at evmain.cc:33
#43 0x0000555555590b13 in main (argc=4, argv=0x7fffffffdbe8) at main.cc:31
Reply all
Reply to author
Forward
0 new messages