Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the debugging method of C++ memory leak?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the debugging mode of C++ memory leak". In daily operation, I believe many people have doubts about the debugging mode of C++ memory leak. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt of "what is the debugging mode of C++ memory leak?" Next, please follow the editor to study!

C++ and other high-level languages, need to manage their own memory, large project calls, easy to memory leaks. Malloc/free and new delete are used to request memory release.

When the memory error pops up a strange address, I look confused. I don't know what went wrong, especially when the program exits. You can use third-party tools to locate the solution, or you can find your own solution.

When I was doing neural network training, I came across a BUG. After sending the training picture, I was directly prompted to use illegal memory.

HEAP [Test.exe]: HEAP: Free Heap block 0000015E950E4A60 modified at 0000015E950E4AA0 after it was freed.

The code iterated many times, some of the functions were not tested, and it was not clear what went wrong.

Think of the memory release mode of the request, the request and release are always paired, and the size of the request is known in advance. Add the memory address print when the memory application is released, and when the memory leak reports an error, you can locate the range of the error memory address. I added a memory print function to print out the memory address range and time.

Void DebugHeap (CString name, void * ptr,int size) {long Time = GetCurrentTime (); TRACE ("= > heap name:%s,address [% pgroup% p], time:%d (ms)\ n", name,ptr, (char *) ptr + size,Time);}

Join the DebugHeap in the application / release section of the program

Static Neural * mThis;static FileInfo mFileInfo;Neural * Neural:: GetInstance (MainDlg * parent,DlgCommand * cmd) {if (mThis = = NULL) {mThis = new Neural (parent); mCmd = cmd;} return mThis;} void Neural:: ReleaseInstance () {if (mThis! = NULL) delete mThis;} Neural::Neural (MainDlg * parent) {mFileInfo.mRcv = (unsigned char *) malloc (1024) DebugHeap ("flag1:alloc", mFileInfo.mRcv, 1024);} Neural::~Neural (void) {bWork = FALSE; DebugHeap ("flag1:free", mFileInfo.mRcv, 1024); free (mFileInfo.mRcv);}

Log while the program is running:

= > ThreadRecvProc Start== > ThreadInterrupt StartMainDlg::OnInitDialog () = > heap name:flag1:alloc,address [0000015E95077CD0:0000015E950780D0], time:7870921 (ms) "Test.exe" (Win32): "C:\ Windows\ System32\ clbcatq.dll" has been loaded. Unable to find or open the PDB file. "Test.exe" (Win32): "C:\ Windows\ System32\ edputil.dll" is loaded. Unable to find or open the PDB file. .... Thread 0x2a10 exited with a return value of 0 (0x0). Thread 0x2ae4 exited with a return value of 0 (0x0). "Test.exe" (Win32): "C:\ Windows\ System32\ mswsock.dll" is loaded. Unable to find or open the PDB file. Connect to the server successfully! Thread 0x16c8 exited with a return value of 96 (0x60). Thread 0xa0c exited with a return value of 0 (0x0). "Test.exe" (Win32): "C:\ Windows\ System32\ msctfuimanager.dll" is loaded. Unable to find or open the PDB file. ... = > heap name:flag1:alloc,address [0000015E951577D0:0000015E95157BD0], time:7885500 (ms) Ending:43707= > heap name:flag1:free, address [0000015E951577D0:0000015E95157BD0], time:7885609 (ms) thread 0x2f20 has exited, and the return value is 0 (0x0). = > heap name:flag1:alloc,address [0000015E950E4AA0:0000015E950E4EA0], time:7887265 (ms) analyse:3, D:\ data\ FSSD\ V5\ target.csv= > heap name:flag1:free, address [0000015E950E4AA0:0000015E950E4EA0], time:7887296 (ms) thread 0x2a00 has exited, and the return value is 0 (0x0). Thread 0xe84 exited with a return value of 0 (0x0). HEAP [Test.exe]: HEAP: Free Heap block 0000015E950E4A60 modified at 0000015E950E4AA0 after it was freedTest.exe has triggered a breakpoint.

Failed to apply for [0000015E95077CD0:0000015E950780D0] release for the first time

The second application [0000015E951577D0:0000015E95157BD0] was released successfully.

The third application [0000015E950E4AA0:0000015E950E4EA0] was released successfully.

The error means that the memory is repeatedly freed. 0000015E950E4AA0 address is repeatedly released, which happens to be the address of the third request.

Look at the phenomenon that the address of the first application has been changed, check this part of the code to analyze the reason.

As I expected to apply for memory in the constructor, the destructor releases memory and can be matched. The singleton pattern is added to the code and a global variable mFileInfo is used, which changes with the constructor call. I envisioned that this class could only be used singleton, and some of the code did not use singleton mode for historical reasons. The address location of the application in the singleton has been changed, so it cannot be released correctly. Unified use of singleton mode, this problem is solved.

When C++ has a memory error, printing out the application / release address can locate the problem more quickly and accurately.

At this point, on the "C++ memory leak debugging mode is what" the end of the study, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report