LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-16-2006, 02:07 AM   #1
akzare
LQ Newbie
 
Registered: Aug 2006
Location: Tehran-Iran
Posts: 1

Rep: Reputation: 0
Problem with Segmentation Fault in Linux


Dear Sir/Madam,

Iím developing a source code in C++ for RTU (Remote Terminal Unit). It will gather data of IO modules of RTU and then transmit them to SCADA center with standard communication protocol (IEC-870-5-101). In first step, I developed it in windows in visual C++ environment. For testing its functionality, I arranged test vectors instead of IO cards of RTU in windows. I trigger test vector via timers in windows. Actually, it is running well without exception. I used try catch in any thread in my program. For example I arranged it in DI thread as follows:

// Digital Input process thread
while(Loop){
try{
GetMsgQue()->PopWait(Msg);
Msg.OpenMsg();
bFunctionCode = (DITHREAD_EVENTS_ENUM_) Msg.GetFunCode();
switch(bFunctionCode){
case DITH_EVENT_SHOUTDOWN_: // ShutDown message to Kill thread
Loop = FALSE_S;
DestroyThread(m_pthread_t);

break;
case DITH_EVENT_HWMCHKMSG_: // Hardware Manager Health Check message to DI Thread
...
break;
case DITH_EVENT_PCIFREEZE_: // PCI Freeze message
...

break;
default :
m_pApp->GetGate()->ErrorMsg("DITH, Unknown FunCode received to DI thread!");
}
}
catch(CScadaException* e){
m_pApp->GetGate()->ErrorMsg(e->GetErrorMessage()+"!(DI_ProcessLoop)");
delete e;
}
catch(...){
m_pApp->GetGate()->ErrorMsg("Unknown exception occurred in DI_ProcessLoop!");
}
}

Now, Iím porting it to Linux to run in embedded system (RTU). It is going well so far but I am hitting a segmentation-fault and each time error occurs, my application will shut off and Aborted (Segmentation Fault) is appear on the terminal. It seems, in Windows catch(...) is a fail safe that catches everything, but in Linux catch(...) doesn't seem to catch anything.


My question is how to control this thing? Letís say after error occurs, just prompt the error message and never do that action so that the app will be ok and still running.

Best Regards
Ali
 
Old 08-16-2006, 08:01 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
A seg fault is typically caused by accessing an area of memory that has not been assigned to the program. So it is not a case of avoiding the problem but locating it and modifying the code.

To help you solve teh problem look at something like gdb a debugger that will be able to tell you where the error is occuring.
 
Old 08-16-2006, 11:42 PM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
C++ exceptions vs Microsoft SEH...

Hi, akzare -

I understand your frustration. The problem is that Microsoft has an OS-level feature called "Structured Exception Handling" (SEH) that their VC++ compiler (or, more precisely, their MSVC runtime) uses to implement C++ try/catch blocks.

The result is non-portable code. ANSI C++ try/catch blocks are NOT intended to catch OS-level events like segmentation faults or floating point exceptions. But, let's face it - Microsoft's Visual Studio, SEH-based implementation is really convenient (to say nothing of a lot more efficient)!

Probably the best approach in your case is to use "signal()" to trap OS-level events (SIGSEGV, SIGFPE, etc) that might occur. Perhaps you want to "#ifdef" these calls, so they're only compiled in the non-Windows versions of the code.

Although I wasn't able to find any good links that directly talk about "pure ANSI C++ vs. Microsoft SEH", these should give you the flavor of the problem:

http://www.dbforums.com/t502031.html
http://rds.yahoo.com/_ylt=A0geurvc7u...onhandling.htm

http://msdn.microsoft.com/chats/tran...io_022703.aspx

'Hope that helps .. PSM

Last edited by paulsm4; 08-16-2006 at 11:45 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault -- permission problem? dx0r515t Slackware 13 08-24-2006 02:55 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 09:34 AM
problem with segmentation fault lucs Slackware 2 04-28-2005 09:14 AM
Segmentation Fault Problem luvonmik Linux - Newbie 2 02-14-2004 07:44 PM
Segmentation fault on RedHat Linux 8.0 hemanth_13 Linux - General 2 07-28-2003 05:52 AM


All times are GMT -5. The time now is 07:08 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration