In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to check memory leaks in C++ related knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe everyone read this C++ how to check memory leaks articles will have some gains, let's take a look at it.
I. Foreword
Valgrind on the Linux platform can be very convenient to help us locate memory leaks, because most of the use scenarios of Linux in the development field are running servers, coupled with its open source attributes, relatively speaking, handling problems easily forms a "unified" standard. On the Windows platform, however, the debugging methods used by server and client developers differ significantly. The following is a combination of my practical experience, sorting out common ways to locate memory leaks.
Note: Our analysis premise is the Release version, because in the Debug environment, memory leaks can be analyzed through the memory leak detection function of the VLD library or the CRT library itself, which is relatively simple. And the server has a lot of problems that need to occur under concurrent pressure online, so it is not meaningful to discuss the Debug version of debugging methods.
II. Object count
Method: Count ++ at object construction, -at destruction, print the number of objects at intervals
Pros: No performance overhead and little extra memory. The location is accurate.
Disadvantages: Intrusive approach, requires modification of existing code, and cannot be located for third-party libraries, STL containers, script leaks, etc. because code cannot be modified.
Overloading new and delete
Methods: Overload new/delete, record allocation points (even call stacks), print periodically.
Advantages: Not seen
Cons: Intrusive approach, adding header files to the heads of a large number of source files to ensure overloaded macros override all new/delete. Record allocation points require locking (if your program is multithreaded), and record allocation takes up a lot of memory (also program memory).
Hook Windows API
Method: Use Microsoft's detours library, hook the system that allocates memory Api: HeapAlloc/HeapRealloc/HeapFree (the underlying call to new/malloc), record allocation points, print periodically.
Advantages: non-intrusive method, no need to modify existing files (hook api, allocation and release go to their own hook function), comprehensive inspection, third-party libraries, script libraries and so on can be counted.
Disadvantages: Recording memory requires a lot of memory, and multithreaded environments require locking.
V. Using DiagLeak Detection
Microsoft memory leak analysis tool produced, the principle of the same hookapi way. With LDGraph visualization to display memory allocation data, it is easier to find leaks.
1. Configure Release version in IDE project options to also generate debugging information. When publishing, publish pdb file and exe file together.
2. After the program runs, open LeakDiag and set Symbol path.
3. Log the memory allocation of the target process regularly, and print the allocation growth through LDGraph to find memory leaks.
Pros: Same as hookapi method, non-intrusive modification, no code changes required. Tracking is comprehensive. Visualize the analysis stack!
Disadvantages: performance impact, hook allocation locking, stack traversal. However, it does not consume the target process's own memory.
About "C++ how to check memory leaks" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "how to check memory leaks in C++." If you still want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.