Skip to content

Commit ca4fc99

Browse files
authored
Fix segfault by 1) avoiding any concurrency on m_socket when the link is a reciever by leaving all connexion issue t the thread and 2) checking that the socket is set before using it (#18)
1 parent d7551d1 commit ca4fc99

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

src/sofa/igtlink/sockets/iGTLinkBase.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ namespace sofa::openigtlink
1818

1919
iGTLinkBase::~iGTLinkBase()
2020
{
21-
m_receiverThread ->stopThread();
22-
delete m_receiverThread;
21+
if (m_receiverThread)
22+
{
23+
m_receiverThread ->stopThread();
24+
delete m_receiverThread;
25+
}
2326
}
2427

2528
void iGTLinkBase::addMessageObject(iGTLinkMessageBase *_object)
@@ -86,7 +89,9 @@ namespace sofa::openigtlink
8689
{
8790
if(sofa::simulation::AnimateBeginEvent::checkEventType(event))
8891
{
89-
if(d_componentState.getValue()!=ComponentState::Valid)
92+
// If we are not a sender, then we let the thread deal with connexions to avoid having
93+
// to slow everything with mutex
94+
if(d_sender.getValue() && d_componentState.getValue()!=ComponentState::Valid)
9095
{
9196
if(!tryConnect()) return;
9297
}

src/sofa/igtlink/sockets/iGTLinkClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace sofa::openigtlink
1616
{
1717
public:
1818
iGTLinkClient();
19-
~iGTLinkClient() = default;
19+
~iGTLinkClient();
2020

2121
void init();
2222

src/sofa/igtlink/sockets/iGTLinkClient.inl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ iGTLinkClient::iGTLinkClient()
1212

1313
}
1414

15+
iGTLinkClient::~iGTLinkClient()
16+
{
17+
m_socket->CloseSocket();
18+
}
19+
1520

1621
void iGTLinkClient::init()
1722
{
@@ -50,13 +55,16 @@ bool iGTLinkClient::tryConnect()
5055

5156
bool iGTLinkClient::isConnected()
5257
{
53-
bool connected = m_socket->GetConnected();
58+
bool connected = m_socket && m_socket->GetConnected();
5459
if(!connected)
5560
{
5661
msg_warning(this) << "Socket not connected to hostname : " << d_hostname.getValue() << ":" <<d_port.getValue();
5762
d_componentState.setValue(ComponentState::Loading);
5863
}
59-
d_componentState.setValue(ComponentState::Valid);
64+
else
65+
{
66+
d_componentState.setValue(ComponentState::Valid);
67+
}
6068
return connected;
6169
}
6270

src/sofa/igtlink/sockets/iGTLinkServer.inl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool iGTLinkServer::tryConnect()
4545
m_socket = m_serverSocket->WaitForConnection(10);
4646
if(!m_socket)
4747
{
48-
std::cout << (std::string("No client connected currently on port :") + std::to_string(d_port.getValue())) <<std::endl;
48+
std::cout << "No client connected currently on port :" << std::to_string(d_port.getValue())<<std::endl;
4949
d_componentState.setValue(ComponentState::Loading);
5050
}
5151
else
@@ -69,13 +69,16 @@ bool iGTLinkServer::tryConnect()
6969

7070
bool iGTLinkServer::isConnected()
7171
{
72-
bool connected = m_socket->GetConnected();
72+
73+
bool connected = m_socket && m_socket->GetConnected();
7374
if(!connected)
7475
{
75-
msg_warning(std::string("Socket not connected to port : ") + std::to_string(d_port.getValue()));
7676
d_componentState.setValue(ComponentState::Loading);
7777
}
78-
d_componentState.setValue(ComponentState::Valid);
78+
else
79+
{
80+
d_componentState.setValue(ComponentState::Valid);
81+
}
7982
return connected;
8083
}
8184

0 commit comments

Comments
 (0)