compile error about destructor
-
Hello everyone, When change from __try to try, and __except(GetExceptionCode()) to catch(...), compile can pass. Compile error and code are, Compiling... main.cpp d:\visual studio 2008\projects\test_exception1\test_exception1\main.cpp(30) : warning C4509: nonstandard extension used: 'main' uses SEH and 'foo1' has destructor d:\visual studio 2008\projects\test_exception1\test_exception1\main.cpp(28) : see declaration of 'foo1' d:\visual studio 2008\projects\test_exception1\test_exception1\main.cpp(35) : error C2712: Cannot use __try in functions that require object unwinding
#include #include #include using namespace std; class Foo { public: Foo() { cout << "constructing Foo" << endl; } virtual ~Foo() { cout << "destrucing Foo" << endl; } }; int main() { int* address = NULL; __try{ Foo foo1; (*address) = 1024; } __except (GetExceptionCode()) { cout << "access violation caught" << endl; } return 0; }
regards, George
MSDN said that:
You should not use structured exception handling in functions that use objects with destructors. If an exception occurs, the destructor cannot be called. Use C++ exception handling instead.
I have never used SEH, c++'s exception handling is more powerful, I think :)A Chinese VC++ programmer
-
Which means that the Standard C++ Exception model is more convenient!
Maxwell Chen
I agree Maxwell! But could we come back to the original question please? Suppose we have to maintain some code using structured exception. :-) Why __try/__except does not work but try/catch work with structured exception? regards, George
-
Sorry, I'm not much of an expert on exceptions etc. A weak point in my armoury. I'm old school, and check return values, instead of putting up a blast shield for when things blow up. I probably *should* do both... Iain.
Thanks all the same, Iain! It is welcome if you could share some points in the future. :-) regards, George
-
MSDN said that:
You should not use structured exception handling in functions that use objects with destructors. If an exception occurs, the destructor cannot be called. Use C++ exception handling instead.
I have never used SEH, c++'s exception handling is more powerful, I think :)A Chinese VC++ programmer
No zengkun100, The destructor could be invoked even if it is structured exception, if you change my code from __try to try, and change __except to catch. Here is the output, constructing Foo destructing Foo access violation caught You can see if we change the keyword, structured exception is caught and destructor is called before handler. Not as you quoted -- "If an exception occurs, the destructor cannot be called.". regards, George
-
Hello everyone, When change from __try to try, and __except(GetExceptionCode()) to catch(...), compile can pass. Compile error and code are, Compiling... main.cpp d:\visual studio 2008\projects\test_exception1\test_exception1\main.cpp(30) : warning C4509: nonstandard extension used: 'main' uses SEH and 'foo1' has destructor d:\visual studio 2008\projects\test_exception1\test_exception1\main.cpp(28) : see declaration of 'foo1' d:\visual studio 2008\projects\test_exception1\test_exception1\main.cpp(35) : error C2712: Cannot use __try in functions that require object unwinding
#include #include #include using namespace std; class Foo { public: Foo() { cout << "constructing Foo" << endl; } virtual ~Foo() { cout << "destrucing Foo" << endl; } }; int main() { int* address = NULL; __try{ Foo foo1; (*address) = 1024; } __except (GetExceptionCode()) { cout << "access violation caught" << endl; } return 0; }
regards, George
Try compiling without the /EHsc switch. As opposed to SEH, C++ exception handling model supports "stack unwinding". It is aware of automatic objects, and it calls their destructors. Basically, use C++ exception handling where it is possible, and fallback to SEH only when there are no other ways. See here.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Try compiling without the /EHsc switch. As opposed to SEH, C++ exception handling model supports "stack unwinding". It is aware of automatic objects, and it calls their destructors. Basically, use C++ exception handling where it is possible, and fallback to SEH only when there are no other ways. See here.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Hi DavidCrow, I compile with /EHa in my question and experiment. If you have Visual Studio, you can have a try. :-) 1. If you remove destructor, it is fine; 2. If you change to try/catch, it is fine. I do not know why. regards, George
-
Hi DavidCrow, I compile with /EHa in my question and experiment. If you have Visual Studio, you can have a try. :-) 1. If you remove destructor, it is fine; 2. If you change to try/catch, it is fine. I do not know why. regards, George
George_George wrote:
2. If you change to try/catch, it is fine.
As opposed to SEH, C++ exception handling model supports "stack unwinding". It is aware of automatic objects, and it calls their destructors.
George_George wrote:
I do not know why.
Now you do.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
George_George wrote:
2. If you change to try/catch, it is fine.
As opposed to SEH, C++ exception handling model supports "stack unwinding". It is aware of automatic objects, and it calls their destructors.
George_George wrote:
I do not know why.
Now you do.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Hi DavidCrow, But even if in my sample, we use try/catch, we are still dealing with structured exception -- access violation, and access violation is not a C++ exception. :-) So, when change to try/catch, I do not agree with you we are in C++ exception category. Any comments?
DavidCrow wrote:
As opposed to SEH, C++ exception handling model supports "stack unwinding". It is aware of automatic objects, and it calls their destructors.
regards, George
-
No zengkun100, The destructor could be invoked even if it is structured exception, if you change my code from __try to try, and change __except to catch. Here is the output, constructing Foo destructing Foo access violation caught You can see if we change the keyword, structured exception is caught and destructor is called before handler. Not as you quoted -- "If an exception occurs, the destructor cannot be called.". regards, George
George_George, the answer is very clear now. Different keyword, different exception handling method. Use C++ exception handling whenever possible :)
A Chinese VC++ programmer
-
George_George, the answer is very clear now. Different keyword, different exception handling method. Use C++ exception handling whenever possible :)
A Chinese VC++ programmer
Any rule in C++ mentioned we can not use object with destructor in __try block with /EHa compile option? I can not find. So, I do not agree. :-) regards, George
-
Hi DavidCrow, But even if in my sample, we use try/catch, we are still dealing with structured exception -- access violation, and access violation is not a C++ exception. :-) So, when change to try/catch, I do not agree with you we are in C++ exception category. Any comments?
DavidCrow wrote:
As opposed to SEH, C++ exception handling model supports "stack unwinding". It is aware of automatic objects, and it calls their destructors.
regards, George
George_George wrote:
...we use try/catch, we are still dealing with structured exception...
Not according to everything that I've read on the subject. Microsoft's
__try
/__catch
is SEH. Perhaps you should read this."Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Moving out Foo foo1 will result in the same compile error, my purpose of this post is to find the root cause of compiler error, i.e. at which point I violate the C++ standard. Any ideas, Iain? regards, George
George_George wrote:
at which point I violate the C++ standard
At the point of using MS-specific SEH.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Douglas Adams, "Dirk Gently's Holistic Detective Agency" -
George_George wrote:
...we use try/catch, we are still dealing with structured exception...
Not according to everything that I've read on the subject. Microsoft's
__try
/__catch
is SEH. Perhaps you should read this."Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Yes, DavidCrow. What I do not agree with you is you mentioned when there is structured exception, during stack unwinding, the local object's destructor is not called. Through my experiment, it is not true. You can try the following code and see if structured exception occurs, like access violation, during stack unwinding, destructor is also called. So the question can be asked in two ways, 1. Why __try/__except does not work with structured exception with destructor; 2. Why try/catch work with structured exception with destructor. Now we are talking about (2).
#include #include #include using namespace std; class Foo { public: Foo() { cout << "constructing Foo" << endl; } virtual ~Foo() { cout << "destrucing Foo" << endl; } }; int main() { int* address = NULL; try{ Foo foo1; (*address) = 1024; } catch (...) { cout << "access violation caught" << endl; } return 0; }
regards, George -
Any rule in C++ mentioned we can not use object with destructor in __try block with /EHa compile option? I can not find. So, I do not agree. :-) regards, George
George_George wrote:
So, I do not agree.
You are getting stubborn.
George_George wrote:
Any rule in C++ mentioned we can not use object with destructor in __try block with /EHa compile option?
The C++-Standard is not about MS-specific compiler internal like
/EHa
or__try
Which with its two leading underscores clearly says "I am vendor-specific". Fact is, that you can tell the compiler to use C++ exceptions and you can tell the compiler to use the Standard-predating SEH.Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Douglas Adams, "Dirk Gently's Holistic Detective Agency" -
George_George wrote:
at which point I violate the C++ standard
At the point of using MS-specific SEH.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Douglas Adams, "Dirk Gently's Holistic Detective Agency"Hi jhwurmbach, More detals? I replied with detailed description and verification sampples, http://www.codeproject.com/script/Forums/View.aspx?fid=1647&select=2403023&fr=39#xx2403023xx[^] regards, George
-
George_George wrote:
So, I do not agree.
You are getting stubborn.
George_George wrote:
Any rule in C++ mentioned we can not use object with destructor in __try block with /EHa compile option?
The C++-Standard is not about MS-specific compiler internal like
/EHa
or__try
Which with its two leading underscores clearly says "I am vendor-specific". Fact is, that you can tell the compiler to use C++ exceptions and you can tell the compiler to use the Standard-predating SEH.Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Douglas Adams, "Dirk Gently's Holistic Detective Agency"So jhwurmbach, your point is __try and __except does not work with local object with destructor? Is there a rule in MSDN covering this? Please read to get my analysis, http://www.codeproject.com/script/Forums/View.aspx?fid=1647&select=2403023&fr=35#xx2403023xx[^] regards, George
-
So jhwurmbach, your point is __try and __except does not work with local object with destructor? Is there a rule in MSDN covering this? Please read to get my analysis, http://www.codeproject.com/script/Forums/View.aspx?fid=1647&select=2403023&fr=35#xx2403023xx[^] regards, George
George_George wrote:
Please read to get my analysis,
We are not here to do your work, George. Most of us have jobs, so we help here when we can, not every time you find something that you disagree with. A little due diligence on your part will go a long way.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Yes, DavidCrow. What I do not agree with you is you mentioned when there is structured exception, during stack unwinding, the local object's destructor is not called. Through my experiment, it is not true. You can try the following code and see if structured exception occurs, like access violation, during stack unwinding, destructor is also called. So the question can be asked in two ways, 1. Why __try/__except does not work with structured exception with destructor; 2. Why try/catch work with structured exception with destructor. Now we are talking about (2).
#include #include #include using namespace std; class Foo { public: Foo() { cout << "constructing Foo" << endl; } virtual ~Foo() { cout << "destrucing Foo" << endl; } }; int main() { int* address = NULL; try{ Foo foo1; (*address) = 1024; } catch (...) { cout << "access violation caught" << endl; } return 0; }
regards, GeorgeOkay. SEH is different from C++ exception.
__try __catch
block indicates that this is a SEH, andtry catch
indicates that this is a C++ exception. MSDN said that :in Visual C++ 2005, all objects in scope when the asynchronous exception is generated will not be destroyed even if the asynchronous exception is handled.
where asynchronous exception is SEH. In your code, Foo's destructor will not be called, so compiler generates an error. If you comment this linecout << "destrucing Foo" << endl;
int Foo's destructor, and your code will be compiled.A Chinese VC++ programmer
-
Okay. SEH is different from C++ exception.
__try __catch
block indicates that this is a SEH, andtry catch
indicates that this is a C++ exception. MSDN said that :in Visual C++ 2005, all objects in scope when the asynchronous exception is generated will not be destroyed even if the asynchronous exception is handled.
where asynchronous exception is SEH. In your code, Foo's destructor will not be called, so compiler generates an error. If you comment this linecout << "destrucing Foo" << endl;
int Foo's destructor, and your code will be compiled.A Chinese VC++ programmer
Great, zengkun100! 1.
zengkun100 wrote:
Foo's destructor will not be called, so compiler generates an error. If you comment this line cout << "destrucing Foo" << endl; int Foo's destructor, and your code will be compiled.
No. You can try to comment out the statements in destructor and the compiler error still exists. 2. try/catch can catch structured exception, in my case, access violation is not a C++ exception and is a structured exception, agree? But try/catch can catch it and calling destructor, unwinding stack quite well. You can try (2) by changing my sample from __try to try and __except to catch(...). regards, George
-
George_George wrote:
Please read to get my analysis,
We are not here to do your work, George. Most of us have jobs, so we help here when we can, not every time you find something that you disagree with. A little due diligence on your part will go a long way.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Thanks all the same, DavidCrow! I appreciate your participation. :-) regards, George