开发者

memory access violation error - 0xC0000005

I got memory access violation error sometimes....

but I don't know where the error comes from...

So I reviewed the code and I found some st开发者_运维百科range code...

delete m_p1;
A *a = new A();
a->b = *c;
m_p1 = a; --> strange code.

I think it's a little strange line.... can i use the m_p1 after delete the m_p1?

It can make some memory access error??


Debugging a memory access violation

You're on Windows.

This means that if the error happens on a machine with Visual Studio installed, you'll have an option to open it to debug the error and thus know the exact line where the error happens.

Studying the code

Too little code to answer correctly. Still, one can guess:

delete m_p1;

Are you sure m_p1 is a pointer to a valid object ? (allocated with new, etc.)

A *a = new A();

You must review the constructor for A. Perhaps it does something wrong.

a->b = *c;

This is the line I suspect the more : If c is an invalid pointer (either NULL, or points to some random address), dereferencing it to put the result inside a->b could provoke the error

m_p1 = a; --> strange code.

The funny fact is that this is the only line of code which can't produce the error you got : a is a pointer containing an address, and you can copy addresses around. It's using this address (dereferencing the pointer, etc.) that causes memory access violation.

About m_p1 = a;

This code is not so strange, but it proves you still have issues with pointers.

In C, C++, Java, etc., when you allocate a memory (to put things inside, to create an object, etc.), the allocator (new, malloc, whatever...) will give you a handle. In Java/C#, this is called a reference, and in C/C++, this is a pointer.

Let's imagine you'll want to go to a concert, and the seat number is where you'll sit, and the seat is the real object, where you'll sit and enjoy the show.

  • m_p1 is a paper in your wallet where you want to have exact seat number
  • a is a temporary paper, a napkin, whatever...
  • delete m_p1 is when you resell your place, so the seat number you bought before is not valid anymore. You should not use it anymore.
  • a = new A() is when you buy another place, so you have in a another valid seat number, but for whatever reason, instead of writing in in your wallet (m_p1), you'll write it on a napkin.
  • m_p1 = a is when you eventually copy the seat number from some napkin to your wallet.

The point is that you have:

  • pointers, which contains memory addresses (the pointer value)
  • memory addresses, where you have real objects

Destroying a real object through delete won't destroy the pointer. It only makes the address invalid (the address didn't change. It's just not valid anymore). So you can put in a pointer whatever address you want, even reuse the pointer to contain the address of another object.


What's strange about it? m_p1 is a pointer, and the line in question is assigning another pointer to it. Perfectly legal C++. The code is not C though, so I've removed that tag.


Depending on what platform you're on, there are tools to help you find these kind of problems.

First, if you get a memory access violation problem, you should be able to get something like a core file (in the unix world at least, I'm sure it is called something entirely different on Windows). From that, you should be able to see exactly where the violation happened.

There are also some tools that will help you both with static analysis of your code and runtime checking for memory violations.

coverity, purify and valgrind are a few examples that comes to mind.

If you're on windows, this might be helpful: how can I debug an access violation


delete m_p1 means "m_p1 points to something that was allocated with new in the past. Please get rid of it". It does not mean "please get rid of the pointer m_p1".


Sorry to resurrect this "thread".

An access violation often comes when we try to access or delete already-freed memory. The trick is ; when you know the memory address where the access violation occurs your can create a data breakpoint and wait until it get triggered. After that it will reveal you where and when this memory has been freed or modified.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜