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