In the program at the end of the message, I change the the signal handler
for SIGSEGV and SIGBUS
I do this in a hope that the program will not core dump when there is a
memory failure. If the program is a very complex server which has a array
of services, which also incorporates many thousands of methods. If one
method in one service has a memory problem, instead core dump the whole
server, there will only be an exception throwing and one request failure,
the server will still be running with most of the services available. On
the other hand, software maintainence programmers can debug offline.
I know this is a bold approach, I am wondering if anyone out there thinked
about this or even using this approach. I notice some times Netscape will
disappear due to "bus error", I believe this approach might help to
prevent that.
Anyway, I still need to make sure that this approach be possible
technically, I believe it's OK but still need opinions from net
friends.
-haibing qiao
*****************************
#include <iostream.h>
#include <signal.h>
class CoreDumpErr
{
public:
CoreDumpErr( const char* em, int sig ):errMsg(em),sigType(sig) {};
const char* getErrMsg()
{
return errMsg;
}
int getSigType()
{
return sigType;
}
protected:
const char* errMsg;
int sigType;
;Quote:}
void sigSegHandler(int error)
{
char c;
cout << " enter 'y' to dump core, or any other to continur--->";
cin >> c;
if( c !='y')
{
throw CoreDumpErr("Core err", error);
}
// a dummy class to test the scope, and create a memory violationQuote:}
class scope
{
public:
scope()
{
cout << "CTOR of Scope" << endl;
}
void print()
{
cout << ppp[100000] << " this is" << endl;
}
~scope()
{
cout << "DTOR of scpe" << endl;
}
char* ppp;
;Quote:}
main()
{
scope *sptrLeak, *sptrCore;
// catch SEGV
signal(SIGSEGV,sigSegHandler);
signal(SIGBUS,sigSegHandler);
try
{
scope sval; // this will be automatically destructed
sptrLeak = new scope; // this will be deleted in catch block
sptrCore->print(); // this line will cause memory violation
delete sptrLeak;
}
catch(CoreDumpErr& err)
{
cout << " the error is catched -->" <<err.getErrMsg() << endl;
cout << "signal type is " << err.getSigType() << endl;
// this delete is for the possible memory leak might occur
delete sptrLeak;
}
cout << " now we get out" << endl;
}
// compile on HP : CC +eh xxxx.C
// compile on SGI: CC -exceptions xxxx.C
// compile on SUN: CC xxxx.C
--