SystemThread random segmentation faults when creating a new thread

31 views
Skip to first unread message

Marco Malinverno

unread,
Mar 24, 2020, 7:58:27 AM3/24/20
to ns-3-users
Dear all,
I am writing you as I am encountering a weird issue when using SystemThread inside ns-3.
My goal would be to create an host application (class MyApp), which should create, inside one of its methods, a new thread, using SystemThread, with a callback accepting my object (MyApp) and an additional argument (an integer file descriptor).
This is the relavant part of the code I'm using:

<myApp.h>

class MyApp : public Application
{
public:
 
/**
   * \brief Get the type ID.
   * \return the object TypeId
   */

 
static TypeId GetTypeId (void);


 
MyApp ();


 
virtual ~MyApp ();


 
void StopApplicationNow ();


protected:
 
virtual void DoDispose (void);


private:


 
virtual void StartApplication (void);
 
virtual void StopApplication (void);


 
static void MyThreadCallback(int *pdr);


 
void AnotherMyAppMethod (Ptr<Socket> socket);


 
// Other declarations...
};


<myApp.cc>

.....


#include <unistd.h>
#include "ns3/system-thread.h"


....


namespace ns3
{
 
.....


   
void
   
MyApp::MyThreadCallback(int *pdr) {
    std
::cout << "The value of pdr is: " << *pdr << std::endl;

   
while(true) {
        std
::cout<<"Loop operations..."<<std::endl;
        sleep
(1);
     
}
   
}


     
......

   
void
   
MyApp::AnotherMyAppMethod (Ptr<Socket> socket)
   
{
     
.....
     
     
// The file descriptor to be passed to the callback is stored inside pd[READ_IDX]
     
Ptr<SystemThread> st = Create<SystemThread> (MakeBoundCallback (&MyApp::MyThreadCallback, &(pd[READ_IDX])));
      st
->Start();

     
......


 
}
}


The problem is that sometimes the application runs fine and the thread callback is actually executed, while most of the times the whole simulation crashes with a SIGSEGV.
The crash seem to occur when creating the thread and when ns-3 is internally calling "self->m_callback()" in SystemThread::DoRun (system-thread.c, line 83).
The big issue is that, when running it with 'gdb' or with an IDE (namely, QtCreator), it almost never crashes, while it crashes very often when running it normally, making it quite difficult to debug.

When running it through './waf --run "myapp-test" --command-template="gdb --args %s"', I'm in fact getting the expected output, without any generated SIGSEGV:

[.....]
[New Thread 0x7fffe7a2d700 (LWP 30856)]
The value of pdr is: 4
Loop operations...
Loop operations...
Loop operations...
Loop operations...
Loop operations...
Loop operations...
Loop operations...
Loop operations...
Loop operations...
Loop operations...
[.....]



Do you know why I'm observing this strange behaviour? Is my code wrong when using SystemThread or MakeBoundCallback?

Thank you very much in advance.
Regards,
Marco

Marco Malinverno

unread,
Mar 24, 2020, 10:40:42 AM3/24/20
to ns-3-users
Sorry, I forgot to mention that I'm working on a Ubuntu 18.04 machine, with ns-3.29.

Thank you again,
Regards,
Marco
Reply all
Reply to author
Forward
0 new messages