Thread: compile error about destructor

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    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,

    1>Compiling...
    1>main.cpp
    1>d:\visual studio 2008\projects\test_exception1\test_exception1\main .cpp(30) : warning C4509: nonstandard extension used: 'main' uses SEH and 'foo1' has destructor
    1> d:\visual studio 2008\projects\test_exception1\test_exception1\main .cpp(28) : see declaration of 'foo1'
    1>d:\visual studio 2008\projects\test_exception1\test_exception1\main .cpp(35) : error C2712: Cannot use __try in functions that require object unwinding

    Code:
    #include <iostream>
    #include <excpt.h>
    #include <windows.h>
    
    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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So you can't use structured exception handling with objects that have a destructor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Mats,


    But why using try/catch is ok? It is still structured exception catched.

    Try to replace the __try to try and replace __except to catch(...).

    Quote Originally Posted by matsp View Post
    So you can't use structured exception handling with objects that have a destructor.

    --
    Mats

    regards,
    George

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    try/catch are able to CATCH a SE, but that doesn't mean that they are the same thing, right?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    What is the difference betwen try/catch and __try/__except when dealing with structured exception?

    Why __try/__except is more strict?

    Quote Originally Posted by matsp View Post
    try/catch are able to CATCH a SE, but that doesn't mean that they are the same thing, right?

    --
    Mats

    regards,
    George

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    perhaps this:
    Quote Originally Posted by MSDN
    Structured exception handling works with Win32 for both C and C++ source files. However, it is not specifically designed for C++. You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, it is recommended that you use the C++ exception-handling mechanism (try, catch, and throw statements).
    From:
    http://msdn2.microsoft.com/en-us/lib...19(VS.80).aspx

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM