John
John

Reputation: 6258

C++ access violation crash

When I attempt to access this specific pointer, the application crashes and shows a c0000005 access violation error.

How can I catch this error and keep it from closing the process as I would in C# with a try&catch block. Or how could I check if the access is denied to that area of memory before I use it?

Example:

MyClass** a = (MyClass**)(0x12345678);
a[0]->b = 1;

I am accessing a table of pointers and setting the value of one of the members of the class. This does work, but the issue is that "0x12345678" doesn't always have the classes loaded in that area. The address has a value, but it doesn't point to the correct area of memory and it doesn't hold the value 0.

Keep in mind, this is a DLL that is loaded into a application that I no longer have the source for. So I'm trying to set the settings of the application dynamically.

Upvotes: 0

Views: 2702

Answers (1)

ta.speot.is
ta.speot.is

Reputation: 27214

You can use Structured Exception Handling to trap these sorts of errors. In particular, filter for EXCEPTION_ACCESS_VIOLATION.

Just make sure you know what you're doing when you swallow the exception: if your garbage address points to a guard page, you might see the behaviour described here.

Upvotes: 2

Related Questions