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. Unhandled exception at ......

Unhandled exception at ......

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionperformancetutorial
10 Posts 4 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.
  • P Offline
    P Offline
    pjmvn
    wrote on last edited by
    #1

    Hi all. I have this code: //an object, create as MObject* object = new MObject(); object->value=10; //try to delete to delete memory of this object delete object; //and then check it //this object is not NULL if (object!=NULL){ //its memory is deleted, so below command is error, how to catch this error by //**try{}catch("what is here"){}**, or have other way to catch this error? int j=object->value; ........ } Please help me. Thanks

    S Steve EcholsS 2 Replies Last reply
    0
    • P pjmvn

      Hi all. I have this code: //an object, create as MObject* object = new MObject(); object->value=10; //try to delete to delete memory of this object delete object; //and then check it //this object is not NULL if (object!=NULL){ //its memory is deleted, so below command is error, how to catch this error by //**try{}catch("what is here"){}**, or have other way to catch this error? int j=object->value; ........ } Please help me. Thanks

      S Offline
      S Offline
      Shuang Wu
      wrote on last edited by
      #2

      try{ int j = object->value; } catch(...){ // handling here }


      |-|3llo Wo|2ld

      P S 2 Replies Last reply
      0
      • S Shuang Wu

        try{ int j = object->value; } catch(...){ // handling here }


        |-|3llo Wo|2ld

        P Offline
        P Offline
        pjmvn
        wrote on last edited by
        #3

        Shuang. Wu wrote:

        try{ int j = object->value; } catch(...){ // handling here }

        Thanks, but this does not work. I using VC++ 2005.

        S 1 Reply Last reply
        0
        • P pjmvn

          Hi all. I have this code: //an object, create as MObject* object = new MObject(); object->value=10; //try to delete to delete memory of this object delete object; //and then check it //this object is not NULL if (object!=NULL){ //its memory is deleted, so below command is error, how to catch this error by //**try{}catch("what is here"){}**, or have other way to catch this error? int j=object->value; ........ } Please help me. Thanks

          Steve EcholsS Offline
          Steve EcholsS Offline
          Steve Echols
          wrote on last edited by
          #4

          Deleting an object doesn't invalidate the pointer to the object. You need to do: delete object; object=null; I'm not sure how to check if deleting of the object actually worked...anyone...anyone..


          - S 50 cups of coffee and you know it's on!

          • S
            50 cups of coffee and you know it's on!
            Code, follow, or get out of the way.
          P 1 Reply Last reply
          0
          • P pjmvn

            Shuang. Wu wrote:

            try{ int j = object->value; } catch(...){ // handling here }

            Thanks, but this does not work. I using VC++ 2005.

            S Offline
            S Offline
            Shuang Wu
            wrote on last edited by
            #5

            well, the code int j = object->value; could lead an unexpected result. sometimes exceptions like AV(Access Violation) could be thrown out, but not always.


            |-|3llo Wo|2ld

            1 Reply Last reply
            0
            • Steve EcholsS Steve Echols

              Deleting an object doesn't invalidate the pointer to the object. You need to do: delete object; object=null; I'm not sure how to check if deleting of the object actually worked...anyone...anyone..


              - S 50 cups of coffee and you know it's on!

              P Offline
              P Offline
              pjmvn
              wrote on last edited by
              #6

              Steve Echols wrote:

              You need to do: delete object; object=null;

              Thanks, but if i do this code. The program does bot call: int j=object->value; and the error does not occur.:) Above is a simple example, my problem is very complicate and i can not set NULL to object. Thanks again. -- modified at 2:55 Friday 14th April, 2006

              Steve EcholsS 1 Reply Last reply
              0
              • P pjmvn

                Steve Echols wrote:

                You need to do: delete object; object=null;

                Thanks, but if i do this code. The program does bot call: int j=object->value; and the error does not occur.:) Above is a simple example, my problem is very complicate and i can not set NULL to object. Thanks again. -- modified at 2:55 Friday 14th April, 2006

                Steve EcholsS Offline
                Steve EcholsS Offline
                Steve Echols
                wrote on last edited by
                #7

                Maybe I'm misunderstanding your problem... If you delete an object, you can no longer access that object. I.e. if you do: delete object; You are not allowed to access any member of the object, since it is "gone" (i.e. object->value will return something you don't expect). If you do: delete object; object=null; And then try to access object->value, you will most likely get an access violation (which is correct). This is correct: [By correct, I mean the code works, but don't do it this way, since you know you just deleted the object.] delete object; object=null; if (!object) { // the object is deleted, you can't access it } else { int j = object->value; } Does that make sense?


                - S 50 cups of coffee and you know it's on! -- modified at 3:06 Friday 14th April, 2006

                • S
                  50 cups of coffee and you know it's on!
                  Code, follow, or get out of the way.
                P 1 Reply Last reply
                0
                • Steve EcholsS Steve Echols

                  Maybe I'm misunderstanding your problem... If you delete an object, you can no longer access that object. I.e. if you do: delete object; You are not allowed to access any member of the object, since it is "gone" (i.e. object->value will return something you don't expect). If you do: delete object; object=null; And then try to access object->value, you will most likely get an access violation (which is correct). This is correct: [By correct, I mean the code works, but don't do it this way, since you know you just deleted the object.] delete object; object=null; if (!object) { // the object is deleted, you can't access it } else { int j = object->value; } Does that make sense?


                  - S 50 cups of coffee and you know it's on! -- modified at 3:06 Friday 14th April, 2006

                  P Offline
                  P Offline
                  pjmvn
                  wrote on last edited by
                  #8

                  Steve Echols wrote:

                  This is correct: delete object; object=null; if (!object) { // the object is deleted, you can't access it } else { int j = object->value; }

                  Yes, if i do that my problem will be fixed. But this code is a simple problem. my problem complicate than that, and i don't permission to set object = NULL; i just have permission to access it and check it for existing or deleted. So if you know how to check it for existing(not deleted) please give me that code. try{}catch("what is here"){} or other way? Thanks again.

                  Steve EcholsS 1 Reply Last reply
                  0
                  • P pjmvn

                    Steve Echols wrote:

                    This is correct: delete object; object=null; if (!object) { // the object is deleted, you can't access it } else { int j = object->value; }

                    Yes, if i do that my problem will be fixed. But this code is a simple problem. my problem complicate than that, and i don't permission to set object = NULL; i just have permission to access it and check it for existing or deleted. So if you know how to check it for existing(not deleted) please give me that code. try{}catch("what is here"){} or other way? Thanks again.

                    Steve EcholsS Offline
                    Steve EcholsS Offline
                    Steve Echols
                    wrote on last edited by
                    #9

                    From the code from your first post, you have permission to delete the object, but not set it to null? That's strange. I'd have to see some more code to see what's going on....


                    - S 50 cups of coffee and you know it's on!

                    • S
                      50 cups of coffee and you know it's on!
                      Code, follow, or get out of the way.
                    1 Reply Last reply
                    0
                    • S Shuang Wu

                      try{ int j = object->value; } catch(...){ // handling here }


                      |-|3llo Wo|2ld

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #10

                      In general catch(...) is bad form. A general rule of exception handling is that you should only catch what you expect to be thrown. Steve

                      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