Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. compile error about destructor

compile error about destructor

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studiohelp
31 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G George_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

    Z Offline
    Z Offline
    zengkun100
    wrote on last edited by
    #8

    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

    G 1 Reply Last reply
    0
    • M Maxwell Chen

      Which means that the Standard C++ Exception model is more convenient!


      Maxwell Chen

      G Offline
      G Offline
      George_George
      wrote on last edited by
      #9

      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

      1 Reply Last reply
      0
      • I Iain Clarke Warrior Programmer

        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.

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #10

        Thanks all the same, Iain! It is welcome if you could share some points in the future. :-) regards, George

        1 Reply Last reply
        0
        • Z zengkun100

          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

          G Offline
          G Offline
          George_George
          wrote on last edited by
          #11

          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

          Z 1 Reply Last reply
          0
          • G George_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

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #12

            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

            G 1 Reply Last reply
            0
            • D David Crow

              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

              G Offline
              G Offline
              George_George
              wrote on last edited by
              #13

              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

              D 1 Reply Last reply
              0
              • G George_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

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #14

                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

                G 1 Reply Last reply
                0
                • D David Crow

                  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

                  G Offline
                  G Offline
                  George_George
                  wrote on last edited by
                  #15

                  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

                  D 1 Reply Last reply
                  0
                  • G George_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

                    Z Offline
                    Z Offline
                    zengkun100
                    wrote on last edited by
                    #16

                    George_George, the answer is very clear now. Different keyword, different exception handling method. Use C++ exception handling whenever possible :)

                    A Chinese VC++ programmer

                    G 1 Reply Last reply
                    0
                    • Z zengkun100

                      George_George, the answer is very clear now. Different keyword, different exception handling method. Use C++ exception handling whenever possible :)

                      A Chinese VC++ programmer

                      G Offline
                      G Offline
                      George_George
                      wrote on last edited by
                      #17

                      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

                      J 1 Reply Last reply
                      0
                      • G George_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

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #18

                        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

                        G 1 Reply Last reply
                        0
                        • G George_George

                          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

                          J Offline
                          J Offline
                          jhwurmbach
                          wrote on last edited by
                          #19

                          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"

                          G 1 Reply Last reply
                          0
                          • D David Crow

                            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

                            G Offline
                            G Offline
                            George_George
                            wrote on last edited by
                            #20

                            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

                            Z 1 Reply Last reply
                            0
                            • G George_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

                              J Offline
                              J Offline
                              jhwurmbach
                              wrote on last edited by
                              #21

                              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"

                              G 1 Reply Last reply
                              0
                              • J jhwurmbach

                                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"

                                G Offline
                                G Offline
                                George_George
                                wrote on last edited by
                                #22

                                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

                                1 Reply Last reply
                                0
                                • J jhwurmbach

                                  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"

                                  G Offline
                                  G Offline
                                  George_George
                                  wrote on last edited by
                                  #23

                                  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

                                  D L 2 Replies Last reply
                                  0
                                  • G George_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

                                    D Offline
                                    D Offline
                                    David Crow
                                    wrote on last edited by
                                    #24

                                    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

                                    G 1 Reply Last reply
                                    0
                                    • G George_George

                                      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

                                      Z Offline
                                      Z Offline
                                      zengkun100
                                      wrote on last edited by
                                      #25

                                      Okay. SEH is different from C++ exception.__try __catch block indicates that this is a SEH, and try 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 line cout << "destrucing Foo" << endl; int Foo's destructor, and your code will be compiled.

                                      A Chinese VC++ programmer

                                      G 1 Reply Last reply
                                      0
                                      • Z zengkun100

                                        Okay. SEH is different from C++ exception.__try __catch block indicates that this is a SEH, and try 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 line cout << "destrucing Foo" << endl; int Foo's destructor, and your code will be compiled.

                                        A Chinese VC++ programmer

                                        G Offline
                                        G Offline
                                        George_George
                                        wrote on last edited by
                                        #26

                                        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

                                        Z 1 Reply Last reply
                                        0
                                        • D David Crow

                                          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

                                          G Offline
                                          G Offline
                                          George_George
                                          wrote on last edited by
                                          #27

                                          Thanks all the same, DavidCrow! I appreciate your participation. :-) regards, George

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups