How to deal with Segment Fault __dynamic_cast () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6

I was doing a demo of Omnet++.
I met this Segment Fault and had no idea about it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  #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;
    }
}
...
There were some some code I didn't show it here,while most of them are assignment statements.
And the main GDB backtrace was like this:
1
2
3
4
5
6
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 

Could anyone help me?
> at LEO_EX.cc:277
the line numbers do not correspond with the code that you've posted
don't feel like hunting, provide a proper testcase http://www.eelis.net/iso-c++/testcase.xhtml

> a demo of Omnet++
¿you use an external library? provide links to such library
First, an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

class Base {
public:
  virtual void method(void) = 0;
};

class Foo : public Base {
  void method(void) {
    cout << "foo\n";
  }
};

class Bar : public Base {
  void method(void) {
    cout << "bar\n";
  }
};

void func ( Base *b ) {
  b->method();
}

int main ( ) {
  Foo foo;
  Bar bar;
  func(&foo);
  func(&bar);
}


$ g++ -g foo.cpp
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) b func
Breakpoint 1 at 0x400922: file foo.cpp, line 22.
(gdb) run
Starting program: ./a.out 

Breakpoint 1, func (b=0x7fffffffde40) at foo.cpp:22
22	  b->method();
(gdb) p *b
$1 = {_vptr.Base = 0x400b58 <vtable for Foo+16>}
(gdb) set print object on
(gdb) p *b
$2 = (Foo) {<Base> = {_vptr.Base = 0x400b58 <vtable for Foo+16>}, <No data fields>}
(gdb) bt
#0  func (b=0x7fffffffde40) at foo.cpp:22
#1  0x0000000000400973 in main () at foo.cpp:28
(gdb) frame 1
#1  0x0000000000400973 in main () at foo.cpp:28
28	  func(&foo);


So, when you hit the segfault in the debugger, just use the 'frame' command to navigate to the handleMessage frame, then print out the 'msg' as shown above so you can figure out why it isn't an object of the right type you can dynamic cast.

Segment Fault __dynamic_cast


There is no usage of dynamic_cast in the posted code.

However there are code segments like:

1
2
3
HelloAnswer *answer;
answer = check_and_cast<HelloAnswer *>(msg);
double repeatTime = answer->getValidTime();


but the code for check_and_cast is not provided.

As this returns a pointer, can this pointer ever be nullptr? If yes, then it needs to be tested before being used. Re-referencing a nullptr would cause the issue.
Topic archived. No new replies allowed.