Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

OS vs C++ Exceptions

pingpongpingpong Member Posts: 937
Hello folks,

Using VC++ 6 with both /GX and /EHa (suppot for both C++ and OS exceptions). Now I have this piece of code:
[code]
try
{
some_class* p = new some_class;
delete p;
// p = NULL;
p->some_function_that_access_a_private_member_variable();
}
catch(...)
{
std::cout << "exception caught" << std::endl;
}
[/code]
And my code just runs pass the catch statement, it causes a page fault somewhere else (without being cought) when I try to access anything on the heap again.

If I take the comment out of "p = NULL" then it works fine, the exception is caught.

My question is what's going on here? Why is the exception not being caught on the first place? Doesnt C++ treat OS exceptions the same way as the language exceptions? And if not, any solutions? Prefarably for VC++?

Thanks,
pingpong

Comments

  • kuphrynkuphryn Member Posts: 266
    What exception do you catch? For example, what is in catching parameter?

    try
    {
    }
    catch (???)
    {
    }

    Kuphryn
  • pingpongpingpong Member Posts: 937
    If I can catch it, I would tell you :)

    I'm using catch(...), i.e. catch all. But it does not make any different if I use catch(exception& e) or catch(exception* e) or whatever.

    : What exception do you catch? For example, what is in catching parameter?
    :
    : try
    : {
    : }
    : catch (???)
    : {
    : }
    :
    : Kuphryn
    :

  • DariusDarius Member Posts: 1,666
    : What exception do you catch? For example, what is in catching parameter?
    :
    : try
    : {
    : }
    : catch (???)
    : {
    : }
    :
    : Kuphryn
    :

    Kuphryn: catch(...) means to catch all exceptions.

    pingpong: I don't believe the standard requires an implementation to catch OS exceptions (I'd have to check). I know Borland doesn't catch a NULL pointer access. However, in your case, the reason it wasn't catching the exception is because no exception was generated until later. delete-ing a variable doesn't set it to NULL and accessing a deleted variable isn't illegal it's undefined. Typically accessing a deleted variable won't cause any problems immediately. It will likely still have the exact same value it had before you deleted it. It will probably keep that value until AT LEAST next time you allocate memory. Unless the memory management system went out of it's way to mark the memory as invalid, there's no way for the machine to tell a valid pointer from a dangling pointer.


    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • pingpongpingpong Member Posts: 937
    Thanks to both of you.

    I suppose this is beyound the C++ language capability. I probably need an OS specific solution or a redesign.

    : : What exception do you catch? For example, what is in catching parameter?
    : :
    : : try
    : : {
    : : }
    : : catch (???)
    : : {
    : : }
    : :
    : : Kuphryn
    : :
    :
    : Kuphryn: catch(...) means to catch all exceptions.
    :
    : pingpong: I don't believe the standard requires an implementation to catch OS exceptions (I'd have to check). I know Borland doesn't catch a NULL pointer access. However, in your case, the reason it wasn't catching the exception is because no exception was generated until later. delete-ing a variable doesn't set it to NULL and accessing a deleted variable isn't illegal it's undefined. Typically accessing a deleted variable won't cause any problems immediately. It will likely still have the exact same value it had before you deleted it. It will probably keep that value until AT LEAST next time you allocate memory. Unless the memory management system went out of it's way to mark the memory as invalid, there's no way for the machine to tell a valid pointer from a dangling pointer.
    :
    :
    : "We can't do nothing and think someone else will make it right."
    : -Kyoto Now, Bad Religion
    :
    :

  • abcabc Member Posts: 443
    [b][red]This message was edited by abc at 2002-9-27 17:34:37[/red][/b][hr]
    : Hello folks,
    :
    : Using VC++ 6 with both /GX and /EHa (suppot for both C++ and OS exceptions). Now I have this piece of code:
    : [code]
    : try
    : {
    : some_class* p = new some_class;
    : delete p;
    : // p = NULL;
    : p->some_function_that_access_a_private_member_variable();
    : }
    : catch(...)
    : {
    : std::cout << "exception caught" << std::endl;
    : }
    : [/code]
    : And my code just runs pass the catch statement, it causes a page fault somewhere else (without being cought) when I try to access anything on the heap again.
    :
    : If I take the comment out of "p = NULL" then it works fine, the exception is caught.
    :
    : My question is what's going on here? Why is the exception not being caught on the first place? Doesnt C++ treat OS exceptions the same way as the language exceptions? And if not, any solutions? Prefarably for VC++?
    :
    : Thanks,
    : pingpong
    :


    What you need to use is SEH (aka structured exception handling) to catch this type of OS-raised exceptions. Here are some links with info about it

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndeepc/html/deep03162000.asp
    http://www.microsoft.com/msj/0197/exception/exception.htm
    http://www.microsoft.com/msj/0497/hood/hood0497.htm

    Have fun ;-)


  • pingpongpingpong Member Posts: 937
    abc,

    Perfect! Used the info from that link and got it to work.

    Thank you very much.

    : [b][red]This message was edited by abc at 2002-9-27 17:34:37[/red][/b][hr]
    : : Hello folks,
    : :
    : : Using VC++ 6 with both /GX and /EHa (suppot for both C++ and OS exceptions). Now I have this piece of code:
    : : [code]
    : : try
    : : {
    : : some_class* p = new some_class;
    : : delete p;
    : : // p = NULL;
    : : p->some_function_that_access_a_private_member_variable();
    : : }
    : : catch(...)
    : : {
    : : std::cout << "exception caught" << std::endl;
    : : }
    : : [/code]
    : : And my code just runs pass the catch statement, it causes a page fault somewhere else (without being cought) when I try to access anything on the heap again.
    : :
    : : If I take the comment out of "p = NULL" then it works fine, the exception is caught.
    : :
    : : My question is what's going on here? Why is the exception not being caught on the first place? Doesnt C++ treat OS exceptions the same way as the language exceptions? And if not, any solutions? Prefarably for VC++?
    : :
    : : Thanks,
    : : pingpong
    : :
    :
    :
    : What you need to use is SEH (aka structured exception handling) to catch this type of OS-raised exceptions. Here are some links with info about it
    :
    : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndeepc/html/deep03162000.asp
    : http://www.microsoft.com/msj/0197/exception/exception.htm
    : http://www.microsoft.com/msj/0497/hood/hood0497.htm
    :
    : Have fun ;-)
    :
    :
    :

  • Justin BibJustin Bib USAMember Posts: 0

    _________ / http://forcoder.org / free ebooks and video tutorials about { PL/SQL Objective-C C# Visual Basic .NET Java MATLAB Scratch Swift Assembly Visual Basic C++ Go R Python C PHP Delphi Perl JavaScript Ruby Logo ABAP Clojure Hack SAS Lua D Alice Fortran Julia Ada Crystal FoxPro COBOL Prolog F# LabVIEW Kotlin ML Scala Erlang Dart VBScript Bash Transact-SQL Awk Apex Lisp Rust Scheme } __

Sign In or Register to comment.