I have a random segmentation fault when I’m calling the function dispatch_handler when I’m receiving a message approximately bigger than 500 bytes. I’m using this class since a while and I never had problem with it but since I’m now having a bigger message I got segmentation fault pretty often. The size of the message defined is 1024 bytes and the maximum number of parts is 65536 so the problem is not there. I can receive millions of small messages without any problem but when I got a bigger message I never know if I will get a segmentation fault.
This is the code I have that is failing:
//------------------------------------------------------------------------
IpcServer *IpcServer::CreateIpcServer(
const std::string & p_strInName)
throw( eIpcError )
{
// create the server
IpcServer *l_poServer = new IpcServer;
assert(l_poServer);
l_poServer->m_strServerName=p_strInName;
// initialize the mutex for accessing the priority_queue
// this may not be necc. but I did not find positive information
// about QNX libraries telling me that priority_queue is thread safe
if ( pthread_mutex_init( &l_poServer->m_oQueueLock, NULL ) != EOK )
{
delete( l_poServer );
throw( IPC_MUTEX_INIT_FAIL );
}
// initialize the semaphore , sem name must start with a /
if ( sem_init( &l_poServer->m_oMsgAvailSem, 0, 0 ) != EOK )
{
delete( l_poServer );
throw( IPC_SEM_INIT_FAIL );
}
// get the IPC server up
try
{
l_poServer->InitResMgr( p_strInName );
}
catch( eIpcError err )
{
delete( l_poServer );
throw( err );
}
// and start the thread
pthread_attr_t attr;
pthread_attr_init( &attr );
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
if ( pthread_create( &l_poServer->m_oServerThread, &attr, ServerThread, reinterpret_cast<void *>( l_poServer )) != EOK )
{
delete( l_poServer );
throw( IPC_THREAD_INIT_FAIL );
}
return( l_poServer );
}
//------------------------------------------------------------------------
void IpcServer::InitResMgr(
std::string inName )
throw( eIpcError )
{
// create the dispatch interface
mdispatch = dispatch_create();
if ( mdispatch == NULL )
{
throw( IPC_CREATE_DISPATCH_FAIL );
}
// set the resmgr attributes
memset( &mresmgr_attr, 0, sizeof( mresmgr_attr ));
mresmgr_attr.nparts_max = MAX_MESSAGE_PARTS;
mresmgr_attr.msg_max_size = MAX_MESSAGE_SIZE;
// setup default I/O functions to handle open/read/write
iofunc_func_init( _RESMGR_CONNECT_NFUNCS, &mConnectFuncs, _RESMGR_IO_NFUNCS, &mIoFuncs );
// setup the attributes for the entry in the filesystem
const int PERMISSIONS = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ;
iofunc_attr_init( &mIoFuncAttr, S_IFREG | PERMISSIONS, 0, 0 );
// Display statistics following a "cat" command on the cmd line.
mIoFuncs.read = IpcServerRead;
// and attach to the filesystem
mresmgr_id = resmgr_attach( mdispatch, &mresmgr_attr, inName.c_str(), _FTYPE_ANY, _RESMGR_FLAG_SELF,
&mConnectFuncs, &mIoFuncs, &mIoFuncAttr );
// check for failure
if ( mresmgr_id == -1 )
{
throw( IPC_RESMGR_ATTACH_FAIL );
}
// setup the message callback
memset( &mmessage_attr, 0, sizeof( mmessage_attr ));
mmessage_attr.nparts_max = MAX_MESSAGE_PARTS;
mmessage_attr.msg_max_size = MAX_MESSAGE_SIZE;
mmessage_attr.flags = MSG_FLAG_DEFAULT_FUNC; // take all messages
// attach the message callback
mmessage_id = message_attach( mdispatch, &mmessage_attr, _IO_MAX+1, _IO_MAX+1, MessageCallback, reinterpret_cast<void *>( this ));
if ( mmessage_id == -1 )
{
throw( IPC_MSG_ATTACH_FAIL );
}
// and finally the context for the dispatch layer to use
mdispatch_context = dispatch_context_alloc( mdispatch );
if ( mdispatch_context == NULL )
{
throw( IPC_DISPATCH_CONTEXT_FAIL );
}
}
//------------------------------------------------------------------------
void *IpcServer::ServerThread(
void *inArg )
{
TRACETHREAD(“IpcMsgServer”);
//dispatch_context_t *ctp_ret;
IpcServer *server = reinterpret_cast<IpcServer *>( inArg );
while( true )
{
server->mdispatch_context = dispatch_block( server->mdispatch_context );
if ( server->mdispatch_context != NULL )
{
[color=red] // This is where it is failing.
dispatch_handler( server->mdispatch_context );
}
else
{
cout<<“IPCServer::Dispatch context is NULL”<<endl;
}
}
}
Sorry for no indentation, it seems that this text doesn’t support tabs.
Thanks for any help