C++ Any advice on tracking down Access Violations?

access-violation, c++, class, destructor, instance

Solution

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:

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

XMEM

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!

Problem

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.

Original source