473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Access violation reading location exception

39
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.
Expand|Select|Wrap|Line Numbers
  1.     try 
  2.     {
  3.         if ( xyz ) // crashes here
  4.         {
  5.             abc = xyz->ID;
  6.         }
  7.     }
  8.  
  9.     catch (...)
  10.     {
  11.         //exception
  12.     }
  13.  
Any suggestion on how to get it caught?

Thanks!
Nov 1 '07 #1
13 26597
Banfa
9,065 Expert Mod 8TB
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.
Nov 1 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 1 '07 #3
ycinar
39
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.
Nov 1 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Post a little more code around this area. Also the struct/class you are using.
Nov 1 '07 #5
ycinar
39
the code is really all over the place.. but here is the most relevant part from the header file.

Expand|Select|Wrap|Line Numbers
  1. class APerson {
  2. public:
  3. APerson(Person* thePerson);
  4. virtual ~APerson();
  5.  
  6. void SetPerson(Person* thePerson) 
  7.                    { 
  8.                     xyz = thePerson;
  9.                    }
  10.  
  11. private:
  12.     Person* xyz;
  13.  
  14.  
xyz must be get deleted somewhere, and that must be the reason why it is crashing.
Nov 2 '07 #6
ycinar
39
Can anyone advise on this please?

I have got a pointer like this

Expand|Select|Wrap|Line Numbers
  1. APerson* xyz = NULL;
  2.  
  3. 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

Expand|Select|Wrap|Line Numbers
  1. if (xyz = 0xfeeefeee) 
  2. // do something
so what is the syntax for the if above? How can I check the address of a pointer?
Nov 2 '07 #7
ycinar
39
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!
Nov 2 '07 #8
ycinar
39
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:
Expand|Select|Wrap|Line Numbers
  1. if (xyz == 0xfeeefeee)
  2. // do something
is there a special meaning for the address 0xfeeefeee as it crashes the program for me there?
Nov 2 '07 #9
sicarie
4,677 Expert Mod 4TB
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.
Nov 2 '07 #10
weaknessforcats
9,208 Expert Mod 8TB
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(Person* 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.

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]
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.
Nov 2 '07 #11
ycinar
39
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?
Expand|Select|Wrap|Line Numbers
  1. if (xyz ==0xfeeefeee)
  2. {
  3. //do something
  4. }
Nov 5 '07 #12
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 5 '07 #13
Banfa
9,065 Expert Mod 8TB
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
Nov 5 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Nasser | last post by:
Hello, I am coding a mathematical model with VC++. When I debug the code, I face with no erroe, but during executing, I face with below error "Unhandled exception at 0x0040c275 in Tar.exe:...
0
by: Bruce Pataki | last post by:
I am creating an MFC application with activeX document server support. The application runs perfectly fine when i run as a standalone application. But when i run the application in Internet...
3
by: Fred | last post by:
I receive this message under certain conditions: 1. "Unmanaged Code Debugging" is checked in Property Pages. 2. Certain routines are "stressed." The error does not appear unless loops are...
2
by: bhreddy | last post by:
Hi All, Can someone help me out how can I resolve the error "0xC0000005: Access violation reading location 0x513112f4"? Steps I followed... 1. I ran the application at DOS prompt 2. After...
1
by: Yanping Zhang | last post by:
Here are more details about my codes, please help! The function declared in C: typedef void (WINAPI *PLEARNCALLBACKPROC) (unsigned int progress, unsigned int sigQuality, unsigned long...
1
by: =?Utf-8?B?c2F0aGVlc2t1bWFy?= | last post by:
In my project i have a component named PTS Engine which is developed in VC++ ..Net 2003. And it is migrated to VC++.NET 2005 version. Initially this migration process has coded and tested in VC++...
3
by: raghunadhs | last post by:
Hi all, i have developed a ".dll" in vc++, regarding to find out CRC32. and in my V.B 6.0 application i am calling that .dll. If i run my v.b application it is working means.. i am able to find...
3
by: Pinux | last post by:
Hi, I am writing a multi-threads encryption application. The idea of the code is to create a number of threads to encrypt files. I have a thread pool say the maximum threads is 10. If the number...
2
by: alexandertfg | last post by:
I finished working on my first draft for my email homework for my C++ class but when debugging I recieved a Access violation reading location 0x00370000 error. I know that it has to do with a stack...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.