Segmentation Fault or compilation error?

Hi, i have a very weird fatal exception happening very rarely on my application (compiled under gcc - Ubuntu linux)

it seems that this this function :

bool GetConnect( ) {return m_Connected;}

causes a Segmentation Fault sometimes (not all the times)

No the instance of the class is not deleted when i call that function.
I tried declaring m_Connected as volatile bool m_Connected but still the error happens.
several different threads also access the function that calls this function.

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
bool CPDC :: CBoolExpr(Clients *client)
{
	if(!client)
	{
		return false;
	}
	
	if(client->m_Deleting) // before we delete our instance of Clients* we set this flag. the instance is deleted 240 seconds later.
	{
		
		return false;
	}
	
	if( !client->m_Socket )
	{
	    return false;
	}
	
	cout << "CBE4" << endl;
	if( !client->m_Socket->GetConnected() )
	{
		return false;
	}

	cout << "CBE5" << endl;
	if(client->m_Socket->HasError() )
	{
		return false;
	}

	cout << "CBEE" << endl;
	return true;
}


it always crashes when it outputs CBE4.
to make sure that another thread doesnt crash the application i also tried creating the other threads in completely different timings. It crashes every time on the same line. Although it makes no sense at all..

Did i miss anything?
Last edited on
Try to set up an environment that ALWAYS crashes where the problem is, and then run it through GDB.
(If you haven't used gdb, here is a basic run through)

compile your code in debug mode

in the console

gdb PROGRAM_NAME
...
(gdb) run


This will run your program... keep running it until it gives a segmentation fault.

When there is a segfault.. run
backtrace


This will help you track whether it's your code or a bad version of stdlib
If you can set up an environment where is ALWAYS crashes, you're already 75% of the way there.

You seem to have an interesting shut down mechanism. That client Deleting business looks very odd. Can I assume that you have a number of race conditions within the app that have been causing other crashes and you've taken such measures to "fix" them?
Last edited on
the application crashes within 24 hours.

it takes 1 hour to 24 hours (depends on server traffic)

1
2
3
4
5
6
7
8
9
10
11
			CTCPSocket *NewSocket = pdcc->m_CTCPS->Accept( (fd_set *)&fd );

			if( NewSocket )
			{
				NewSocket->SetNoDelay(true);
				Clients *client = new Clients(NewSocket, pdcc);
				client->m_Identified = false;
				client->m_Chat = pdcc->m_DefaultChat;
				CONSOLE_Print("[SERVER] New client has connected to the server [ " + NewSocket->GetIPString( ) + "].");
				m_Clients.push_back(client);
			}


client connects to the server...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	for(vector<Clients*> :: iterator i = m_Clients.begin( ); i != m_Clients.end( );)
	{
		if( (*i)->m_Socket && (*i)->m_Socket->GetConnected( ) && !(*i)->m_Socket->HasError( ) )
		{
			(*i)->m_Socket->SetFD(&fd, &send_fd, &nfds);
			nfds++;
			i++;
		}
		else
		{
			cout << "[PROXY] A client connection was lost." << endl;
			CleanUpUser((*i));
			i = m_Clients.erase( i );
		}
	}


client connection lost

1
2
3
4
5
6
7
void CPDC :: CleanUpUser(Clients *client)
{
	client->m_DeleteTime = GetTime();
	client->m_Deleting = true;
	m_ClientsDeleting.push_back(client);

}


dereferrencing the instance safely later

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	for(vector<Clients*> :: iterator i = m_ClientsDeleting.begin( ); i != m_ClientsDeleting.end( ); )
	{
		if( (*i) )
		{
			if( GetTime() - (*i)->m_DeleteTime >= 240 )
			{
				delete *i;
				i = m_ClientsDeleting.erase( i );
			}
			else
				i++;
			}
		else
			i = m_ClientsDeleting.erase( i );
	}


seg fault happens on the CBoolExpr( ) function that validates the Client
i use that whenever a client sends a 'special' package and when the client disconnects ( we got chat channels that remove Clients if their delete flag is on. )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    for(vector<CChat*> :: iterator i = m_ChatRooms.begin( ); i != m_ChatRooms.end( );)
	{
		if(!(*i))
			i = m_ChatRooms.erase( i );
		else
		{
			for(vector<Clients*> :: iterator j = (*i)->m_Users.begin( ); j != (*i)->m_Users.end( );)
			{
				if( !CBoolExpr( (*j) ) )
				{
					Event_UserLeft2( (*j), (*i) );
					j = (*i)->m_Users.erase( j );
				}
				else
					j++;
			}
			
			i++;
				
		}
	}



although i think i fixed it by making that function where we see the segmentation fault to 'non-virtual' and 'volatile' (it was a virtual bool function)

if that's the case... then it means that virtual and multi-threading can not work together.
Ok, try setting up a local server/client system that crashes instantly
server is still up since yesterday night, so i guess that was it? ^^

virtual functions and multiple threads accessing it = segmentation fault?

first time i see such thing...
that virtual and multi-threading can not work together.
Not true at all.
Topic archived. No new replies allowed.