Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check memory corruption in C++

I have an issue in my project. Sometimes execution fails with the error "unabe to access a reading/writing location xxxxx". I know it is a memory corruption error. But how can I trace that error. It is failing at memory deallocation of a variable , if I comment that line it is processing fine. That is , the the memory for this variable has got released already. Earlier it was working fine, I added code recently but i didnt change anything related to this. How can I check the memory corruption fo this.

like image 346
Amaravathi Avatar asked Apr 02 '12 17:04

Amaravathi


People also ask

How do you find memory corruption?

Detecting Memory Corruption. You can detect memory block overrun and underrun errors with either guard blocks or Red Zones. Select Guard allocated memory from Advanced Memory Debugging Options. With guards on, MemoryScape adds a small segment of memory before and after each block that you allocate.

What causes memory corruption in C?

Pointer variable can point to a invalid memory location which can cause access violation and a crash. Memory corruption may occur because of poor array buffer handling or some abnormal runtime use-cases.

How do I know if my memory is overwriting?

By implementing your own memory allocator you can turn on a special debug mode where you write some known pattern into freed memory. You then periodically check all free memory to see if the pattern has been overwritten. If it has you assert or log or something.

What is memory corruption error?

Definition: Memory corruption can be described as the vulnerability that may occur in a computer system when its memory is altered without an explicit assignment. The contents of a memory location are modified due to programming errors which enable attackers to execute an arbitrary code.


1 Answers

Use valgrind if you're under linux, it will show you exactly what's happening if you stomp your memory.

On windows: see here.

like image 156
Gui13 Avatar answered Nov 01 '22 07:11

Gui13