Access violation reading location exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ycinar
    New Member
    • Oct 2007
    • 39

    Access violation reading location exception

    A quick question:

    Why doesn't the following code catch the Access violation reading location exception?

    it crashes on line if ( xyz ) with an Access violation reading location exception. Is not catch supposed to catch it? Practically, it doesnt.
    [code=cpp]
    try
    {
    if ( xyz ) // crashes here
    {
    abc = xyz->ID;
    }
    }

    catch (...)
    {
    //exception
    }
    [/code]
    Any suggestion on how to get it caught?

    Thanks!
    Last edited by sicarie; Nov 1 '07, 01:25 PM. Reason: Code tags are [code=cpp] and (your code here) [/code]. Please use them.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The C++ try{...} catch {...} methodology catches software exceptions. Accessing an invalid memory location is a hardware exception (that is it is not obviously wrong in the software it is only when you look at the location and find no hardware that the error is realised.

    You could try this sort of exception using a signal but that is not going to help very much as following signal you would have to peform some major reset operation of your program (or exit it).

    It would be much better not to create the situation in the first place, you can do this by always ensuring that your pointers either point to a valid location or have been set to NULL.

    This should not be hard to do, intiialise them to NULL, then you can assign to them to point them to valid memory. When that memory is no longer valid (it has been deleted or gone out of scope) reset there value back to NULL.


    It is dangerous to leave apointer floating with a random value.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by ycinar
      try
      {
      if ( xyz ) // crashes here
      {
      abc = xyz->ID;
      }
      }

      catch (...)
      {
      //exception
      }
      This code shouild not crash on the if statement if xyz is a pointer. The if statement does a test for true or false only and does not access the location in the pointer.

      What can happen is that you never initialized xyz so that it is non-zero (hence true) but contains a garbage value. That will bring you down.

      Comment

      • ycinar
        New Member
        • Oct 2007
        • 39

        #4
        Originally posted by weaknessforcats
        This code shouild not crash on the if statement if xyz is a pointer. The if statement does a test for true or false only and does not access the location in the pointer.

        What can happen is that you never initialized xyz so that it is non-zero (hence true) but contains a garbage value. That will bring you down.
        I am surprised too that it crashes at if statement.

        it is more understandable crashing at xyz->ID ...

        but it crashes at if statement for some reason. It is definitely initialized by the way.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Post a little more code around this area. Also the struct/class you are using.

          Comment

          • ycinar
            New Member
            • Oct 2007
            • 39

            #6
            the code is really all over the place.. but here is the most relevant part from the header file.

            Code:
            class APerson {
            public:
            APerson(Person* thePerson);
            virtual ~APerson();
            
            void SetPerson(Person* thePerson) 
                               { 
                                xyz = thePerson;
                               }
            
            private:
            	Person* xyz;
            xyz must be get deleted somewhere, and that must be the reason why it is crashing.

            Comment

            • ycinar
              New Member
              • Oct 2007
              • 39

              #7
              How to check an address of a pointer?

              Can anyone advise on this please?

              I have got a pointer like this

              Code:
              APerson* xyz = NULL;
              
              xyz = abc->Caller();
              abc is initialized somewhere publicly. Sometimes abc->Caller() returns 0xfeeefeee value and which will cause xyz to crush a later line.

              So what I would like to is, I want to

              Code:
              if (xyz = 0xfeeefeee) 
              // do something
              so what is the syntax for the if above? How can I check the address of a pointer?

              Comment

              • ycinar
                New Member
                • Oct 2007
                • 39

                #8
                is there a way to check the address value of a pointer?

                if (xyz == 0xfeeefeee)
                // do something

                i wonder if there is such a thing, if so, what would the syntax for the if statement above?

                thanks!

                Comment

                • ycinar
                  New Member
                  • Oct 2007
                  • 39

                  #9
                  Originally posted by RedSon
                  Its probably just a memory dump which means that you are going to need a good text editor to see the hex information. Try Notepad++ or TextPad.
                  Thanks RedSon,

                  another quick question

                  is there a way to check the address of a pointer? something like:
                  Code:
                  if (xyz == 0xfeeefeee)
                  // do something
                  is there a special meaning for the address 0xfeeefeee as it crashes the program for me there?

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    You know, if that was a new question, I'd suggest a new thread, but as that is already in a thread, I would recommend waiting for someone to answer it instead of being impatient and posting it all over, so I'm going to move it back over to your thread.

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      Originally posted by ycinar
                      the code is really all over the place.. but here is the most relevant part from the header file.


                      Code: ( text )
                      class APerson {
                      public:
                      APerson(Person* thePerson);
                      virtual ~APerson();

                      void SetPerson(Perso n* thePerson)
                      {
                      xyz = thePerson;
                      }

                      private:
                      Person* xyz;


                      xyz must be get deleted somewhere, and that must be the reason why it is crashing.
                      If xyz is ever deleted then the APerson object is trash. This is one reason to not use pointers as data members. Use handles instead. There's an article in the C/C++ Articles forum on Handle Classes that addresses this issue.

                      If xyz is 0 or has a value outside your process address space (like it was never initialized) you will crash when you use it.

                      If xyz is deleted, the deleter has to notify all APerson objects that have the pointer that is going to be deleted. This is a big, big job. Again, a handle rather than a pointer will solve this.

                      Originally posted by ycinar
                      Can anyone advise on this please?

                      I have got a pointer like this


                      Code: ( text )
                      APerson* xyz = NULL;

                      xyz = abc->Caller();


                      abc is initialized somewhere publicly. Sometimes abc->Caller() returns 0xfeeefeee value and which will cause xyz to crush a later line.

                      So what I would like to is, I want to


                      Code: ( text )
                      if (xyz = 0xfeeefeee)
                      // do something


                      so what is the syntax for the if above? How can I check the address of a pointer?
                      This looks like a case where abc->Caller() is returning an uninitialized pointer. Don't bother hard-coding an address value to check as the garbage address will be different from one execution to the next.

                      Your solution is to make sure that Caller() does not return garbage.

                      When it comes to using pointers in C++ classes:
                      1) you must initialize them. Period.
                      2) they should point to items on the heap so that you are assured that what they point to won't be deleted by the compiler when some function completes
                      3) they cannot be deleted unless the deleter knows it is deleting the last copy of the pointer in the program. A handle covers this condition.

                      Finally,
                      [quote=ycinar]
                      Originally posted by ycinar
                      the code is really all over the place..
                      is a bad sign. Try to get all the code for a class into one source file and the class declaration into one header file. Use separate headers for different classes and separate source files for different classes.

                      Comment

                      • ycinar
                        New Member
                        • Oct 2007
                        • 39

                        #12
                        Originally posted by weaknessforcats
                        This looks like a case where abc->Caller() is returning an uninitialized pointer. Don't bother hard-coding an address value to check as the garbage address will be different from one execution to the next.
                        The address doesnt differentiate from one execution to the next. I know best way is not to return this value which seems non-trivial.

                        so im still interested in learning how to check the address of pointer?
                        Code:
                        if (xyz ==0xfeeefeee)
                        {
                        //do something
                        }

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          Originally posted by ycinar
                          so im still interested in learning how to check the address of pointer?

                          Code: ( text )
                          if (xyz ==0xfeeefeee)
                          {
                          //do something
                          }
                          then you will need to type cast the pointer to an unisigned int to make the test.

                          Be advised, this is the wrong approach. The correct thing to do is to see why Caller() is returning garbage.

                          Comment

                          • Banfa
                            Recognized Expert Expert
                            • Feb 2006
                            • 9067

                            #14
                            You should not rely on the value 0xfeee. This is a value that the Microsoft Runtime Debug library assigns to memory as it is deallocated to aid in bug tacking.

                            This means that

                            1. On a non-Microsoft system the test may not work

                            2. On a Microsoft system built for production the test may not work

                            3. Using a future version the Microsoft Debug Library the test may not work

                            Comment

                            Working...