C ++ Any tips on tracking access violations?

I am having trouble finding an access violation in my program. This happens when the destructor is called a third time, exactly when the destructor seems to end.

I spent hours trying to track this, so I am looking for further advice on what I can do. I create an instance of the class with the 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 to figure out what was in these places of memory?

The call stack window shows the following (in reverse order, when I inserted it in chronological order, from earliest to last):

 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 () String 214 + 0x5 bytes C ++

My best guess in this information is that there is some kind of string variable causing the problem? Does anyone have any tips on interpreting this information?

Any other tips would also be helpful, thanks in advance.

I am coding for Windows 7 and using Visual Studio 2010 Professional.

+6
source share
3 answers

I had problems tracking memory errors before using BoundsChecker (now part of Borland DevPartner) . There are a number of similar products that may also help: HeapAgent and Rational Purify . They seem to be very similar to ValGrind, but they run on Windows.

Here are 3 open source alternatives that might help:

  • DUMA (in appearance you will have to create it yourself for Windows, but README contains some comments about this)

  • Xmem

  • elephant

I have no idea how they work, but they sound very promising and look like they all work on Windows anyway.

This Microsoft memory error management page can also help, and also has links to setting memory breakpoints that can help you find out when your data is first deleted or changed. Good luck

+3
source

Use the Microsoft Heap debugging material and hope that yours is one of the cases for which it was designed. After that, Purify will be the next step.

It is built into Visual Studio and is useful in some cases. If this works, it will mean that IBM will get two or three pockets full of cash for Purify.

Here you can find information

TL DR basically do it

  int tmpFlag = _CrtSetDbgFlag (_CRTDBG_REPORT_FLAG);
 // Turn On (OR) - Keep freed memory blocks in the
 // heap 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 switch the _CRTDBG_CHECK_ALWAYS_DF flag in different places if it is too slow. However, I would run it several times with each heap check to see where the problem is.

+2
source

I wrote this blog with some tips.

http://www.atalasoft.com/cs/blogs/loufranco/archive/2007/02/06/6-_2200_Pointers_2200_-on-Debugging-Unmanaged-Code.aspx

The main thing you need to do is get a crash or an exception while the error code is still on the stack. Many times you get an access violation some time after the code with the error executed and returned, and in fact it can be a long time (in computer time). In this case, it is almost impossible to understand this.

In your case with the problem marked in delete, this is a strong indicator that the heap is damaged, and two common causes in C ++ are double deletion and mixing of array deletion (using delete when you should use delete [] or vice versa).

If you can reproduce it with simple code, I would consider the two problems above. Otherwise, download the Microsoft debugging tools and use gflags +hpa -i program.exe to make the heap much more susceptible to corruption (it will report an error much faster).

+1
source

Source: https://habr.com/ru/post/915151/


All Articles