Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Any advice on tracking down Access Violations?

I've been having issues trying to track down an Access Violation in my program. It occurs when the destructor is called for the third time, exactly when the destructor appears to finish.

I've spent hours trying to track this down so I'm looking for further advice on things I can do. I'm creating the class instance with new and delete operators. The Visual Studio output window shows:

First-chance exception at 0x60e3ad84 (msvcp100d.dll) in WebCollationAgent.exe: 0xC0000005: Access violation writing location 0xabababab. Unhandled exception at 0x60e3ad84 (msvcp100d.dll) in WebCollationAgent.exe: 0xC0000005: Access violation writing location 0xabababab.

Is there anything I can do to try and find out what was in those memory locations?

The call stack window shows the following (in reverse order as I've pasted it in to chronological order, earliest to latest):

Program.exe!Network::`scalar deleting destructor'()  + 0x2b bytes   C++

Program.exe!std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::~basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >()  Line 754 + 0xf bytes    C++

Program.exe!std::_String_val<wchar_t,std::allocator<wchar_t> >::~_String_val<wchar_t,std::allocator<wchar_t> >()  Line 478 + 0xb bytes  C++

msvcp100d.dll!std::_Container_base12::_Orphan_all() Line 214 + 0x5 bytes C++

My best guess at this information is that there's some sort of string variable causing the issue? Does anyone have any advice on interpreting this information?

Any other pieces of advice would also be useful, thanks in advance.

I'm coding under Windows 7 and using Visual Studio 2010 Professional.

like image 470
Interminable Avatar asked May 08 '12 03:05

Interminable


People also ask

What are access violations?

An access violation is a non-specific error that occurs while installing, loading, or playing a game. This error can be caused by the following: an interfering software program (usually an antivirus application), an outdated video card driver, or an outdated version of DirectX.

What causes a read access violation?

A Read or Write Access Violation occurs when the application attempts to read or write memory from a memory address that is invalid. To be valid, the memory page must have a valid state, protection and type. The memory must be in the MEM_COMMIT state.

What is the code of access violation?

An access violation occurs in unmanaged or unsafe code when the code attempts to read or write to memory that has not been allocated, or to which it does not have access. This usually occurs because a pointer has a bad value.

What is a write access violation C++?

As its name says, this error occurs whenever you try to access a location that you are not allowed to access in the first place. In other words, whenever you will try to violate the norms of accessing a writing location set up by the C++ programming language, you will always come across this error.


2 Answers

I've had success tracking memory bugs before using BoundsChecker (now part of Borland DevPartner). There are a number of similar products that might also help: HeapAgent, and Rational Purify. These seem to be much like ValGrind, but work on Windows.

Here are 3 open source alternatives that might assist:

  1. DUMA (by the looks of it, you'll have to build it for Windows yourself, but the README contains some notes on doing that)

  2. XMEM

  3. elephant

I have no idea how these perform, but they sound very promising, and look like they all work on Windows in one way or another.

This Microsoft page on managing memory errors might also help, and also has links to setting Memory breakpoints, which might help you find out when your data is first being deleted or altered. Good luck!

like image 95
craigmj Avatar answered Nov 16 '22 02:11

craigmj


Use the Microsoft Heap Debugging stuff and hope yours is one of the cases for which it was designed. Purify would be the next step after that.

It's built into Visual Studio and is helpful in some cases. If it works it sure beats giving IBM two or three pockets full of cash for Purify.

You can find the info here

TL;DR In main, do this

int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
// Turn On (OR) - Keep freed memory blocks in the
// heap's linked list and mark them as freed
tmpFlag |= _CRTDBG_DELAY_FREE_MEM_DF;

// Turn on memory checking at each heap operation
tmpFlag |= _CRTDBG_CHECK_ALWAYS_DF;

// Set the new state for the flag
_CrtSetDbgFlag( tmpFlag );

You can toggle the _CRTDBG_CHECK_ALWAYS_DF flag at different places if it runs too slow. However, I'd run it a few times with each heap op checked in order to get a feel for where the problem occurs.

like image 32
JimR Avatar answered Nov 16 '22 01:11

JimR