Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Throwing exceptions under Solaris

Posted on 2000-02-18
15
Medium Priority
?
324 Views
Last Modified: 2013-12-05
I have an exception class under NT inside a DLL. When I want to throw this exception, I use "throw new TException ("hello")".

Then I port this class to Solaris, but when the program flow gets to the catch statement, an "Abort" message appears and then the program stops. This doesn't happen under Windows NT : when a cpp throws the exception, the catch statement intercepts the exception and the program flow continues.

This is the code :

** In TException.so file :

class TException : public exception
{
public :
        TException (char *string);
        ~TException ();
}

** In TMyClass.so :

void TMyClass::f ()
{
       throw new TException ("hello");
}

** In main.cpp :

try {
    myclass->f ();
}
catch (TException *e) {
    printf ("catch...\n");

    delete e;
}

The message "catch..." doesn't appear. Appear an "Abort" message.

What's the matter?
0
Comment
Question by:Lucsat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2534465
There's no way to tell from just that.  Could you show us some code?
0
 

Author Comment

by:Lucsat
ID: 2534532
Edited text of question.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2534556
Please do not edit the question once a dialog has started.  It makes it impossible for other experts to follow.

I don't see a problem there.  If you move the code to a single file does the problem go away?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Lucsat
ID: 2534585
Sorry for edit the question. I never use experts-exchange service and I'm a bit lost.

When I move all the code to a single file, then the problem disappears.

Another case : if main.cpp throws a TException exception, the catch statement works correctly.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2534651
>> Sorry for edit the question.
You shouldn't even be given the option to edit after a comment has been posted.  Hopefully that will be fixed in the next version.

That is sounding a lot like it has something to do with the implimentaiton of exceptions and DLLs on solaris.   (i.e it is not a C++ issue, but an OS-specific issue.)  There is a chance that you can get some more info here, but I sort-of doubt it.  Because this is so specific, I would consider gettting help from the compiler manufacturer or OS.  (maybe wait a little first to see what happens here.)  And there is a good chance that help might only be an explanation of why it doesn't work, not how to make it work.

Although I can't image why it wouldn't work...
0
 

Author Comment

by:Lucsat
ID: 2541604
Adjusted points to 50
0
 

Author Comment

by:Lucsat
ID: 2541605
I have found the problem :

When I declared the function that throws the exception, I use "MyClass::f () throws (TException)".

When I deleted the "throws (TException)" statement, then all was OK.

But, I don't understand why under Windows NT this statement functions but under Solaris or Linux doesn't works correctly.

What's the matter?
0
 
LVL 22

Accepted Solution

by:
nietod earned 240 total points
ID: 2542557
That makes sense!  You didn't mention you had exception specifications in your code  (I should have though to ask.)

The problem is you were violating the specification.

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2542574
>> When I declared the function that throws the
>> exception, I use "MyClass::f () throws (TException)".

This specificaton assures that the f() throws only TException (or types that convert to it) or doesn't throw at all.  If you attempt to throw any type that can't be converted to TException, the program won't allow it and will abort the program with an unhandled exception.

But you are throwing TException right?  wrong.  You are throwing "TException *".  This is a diferent type.  If you changed the throw code from

throw new TException ("hello");

to

throw TException ("hello");

Then you should be okay.

By the way, it is often best to throw exceptions by value, not pointer and is usually best to catch excetptions by reference, not value.

Let me know if you have any quesions.
0
 

Author Comment

by:Lucsat
ID: 2545195
Adjusted points to 60
0
 

Author Comment

by:Lucsat
ID: 2545441
There are only one problem : the locality of the throwed object.

When I use

throw TException ("hello")       (internally I store "hello")

in a function, this object only live in this function, and when an external function catch that exception, the "hello" string doesn't exist because in the TException destructor I use delete [] the_string.

0
 
LVL 22

Expert Comment

by:nietod
ID: 2545742
Does TException store a pointer the string?  If so, did you write a working copy constructor (and while you are at it operator =) for the class.  The defaullt versions of these two procedures don't work right when you have data members that are to point to "private" blocks of allocated memory.  And the copy constructor will almost assuredly be used when throwing an exception.
0
 

Author Comment

by:Lucsat
ID: 2549526
The definition of TException are :

class TException
{
public:
   TException (char *pszString);
   ~TException ();

private:
   char *m_pszString;
};

TException::TException (char *pszString)
{
   m_pszString = new char (strlen (pszString) + 1);

   strcpy (m_pszString, pszString);
}

TException::~TException ()
{
   delete [] m_pszString;
}

I don't understand when you say me "write a working copy constructor". How I can to throw an exception using the copy constructor? How I can to change the char pointer to another solution (I'm sure that an static array is a solution, but I like to reserve memory as I need too much...)
0
 
LVL 22

Expert Comment

by:nietod
ID: 2550059
If that is the entire class definition it definitely has a problem.   The problem has nothing to do with exceptions.  This is enherint in the current class design.  i.e. the class behaves poorly with or without an exception.  For example try

int main()
{
   TException A("this is a test.");

   {
      TException B(A);
   }
   return 0;
}

This should yeild an error at the end of main().  Actually this WILL yield  an error at the end of main, the only dobut I have is that your debugger might not pick it up.  try it.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2550072
The problem is that you have not provided a copy constructor so when TException object's are copied the default copy constructor is used.  This copy constructor does not work well for this case.  What it does is copy ever single member of the class.  So as a result the new TException object has the same pointer in m_pszString as the old.  i.e you have two objects that are both sharing the same string data.  when the first object is destroyed it deletes teh string and the 2nd object is left holding a pointer to string data that has been deleted.  

You need to write a copy constructor for this class that copies the string data.  Then it will be safe to make copies of TException.   (And the compiler makes copies when you throw--as well as other times.)

The same problem exists for operator =.  I you ever do an = with Texception you will have a problem too.  So you need to define you own operator = or make sure you never use operator = (declare it private for example) on this class.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question