473,735 Members | 6,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to identify who cause memory leak

Is there any debugging mechanism to find out which part of the code is
causing memory leak?

Dec 8 '05 #1
4 2298

NewToCPP wrote:
Is there any debugging mechanism to find out which part of the code is
causing memory leak?


http://freshmeat.net/search/?q=memor...&Go.x=0&Go.y=0

Dec 8 '05 #2
NewToCPP wrote:
Is there any debugging mechanism to find out which part of the code is
causing memory leak?


Not in the C++ language itself. There are many third party utilities
that do so, but they are dependent upon your platform.

I suggest that you ask in a newsgroup more relevant to your platform,
such as either one of the microsoft.publi c.* newsgroups or
comp.unix.progr ammer, or whatever the mac programming newsgroups are.

There will be developers there who have dealt with this sort of thing,
and can tell you better what's available.

*** ON THE OTHER HAND ***

Many memory leaks can be avoided by not using dynamic memory if at all
possible, or by using containers and smart pointers.

e.g. If you have a pointer that points to a single instance, use the
smart pointer of your choice. If you have a pointer to multiple
elements, which you're using as a dynamic array, then try std::vector
instead. You should also use std::vector instead of arrays. If you're
allocating char* to use as strings, then you should be using std::string
instead.

By using these elements instead of trying to manage memory yourself, you
will minimize your leakage, because these containers (string, vector)
and smart pointers will manage the memory for you, freeing it when it is
no longer needed.

You should also google for the term RAII (or Resource Acquisition Is
Initialization) .
Dec 8 '05 #3
NewToCPP wrote:
Is there any debugging mechanism to find out which part of the code is
causing memory leak?


It is not a debugging mechanism, but:
If you work on Linux, use the "valgrind" tool. It's the best tool for
this I know about. It works non-invasive (you don't need to change any
code - it works on the executable), which is very nice.

Gabriel

--
Who is General Failure and why is he reading my hard disk?
Dec 9 '05 #4
NewToCPP wrote:
Is there any debugging mechanism to find out which part of the code is
causing memory leak?


You can use the leaktracker program which is a free program that has
the source code available. With the leaktracker program you can run
your program, and when your program exits, the leaktracker program will
automatically write to a file all memory leaks detected, which includes
line number, sourc file name, and even function name where the leak was
created.
The leaktracker program detects leaks created by new, new[], malloc,
realloc, and much more. It also detects resource leaks created by C
functions like fopen.
For windows platforms, it also has resource leak detection for windows
API functions that use HANDLE type.

Check out the following link:
http://code.axter.com/leaktracker.h
If you're target is windows, you can use the above link along with the
DLL and Lib file in the following zip file to track memory leaks:
http://code.axter.com/leaktracker.zip

The program is free, and if you need the source code you can get it via
following link:
http://code.axter.com/leaktracker_dll_source_code.zip

The code has been tested in Windows environment with MS VC++ 6.0, 7.1,
GNU 3.x, and Borland 5.5.

Although it's targeted for Windows, it wouldn't be that hard to port it
to other platforms, since the Windows related part of the code has
#ifdef WIN32 wrap.

For more information, read the entire commented section of the
leaktracker.h file.

I'm also open to any suggestions to make the program better, or more
portable.

Dec 9 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10304
by: Jeff Rodriguez | last post by:
If you realloc, and the new memory has a different address, what happens at the old address? Is that memory basically free()'d, or do you need to keep track of address changes and manually free() ? Jeff
17
4812
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is easier to reproduce (e.g.: remote machine not available). After about 1 day, I get a usage of 300MB of memory. I have used .NET Memory Profiler tool to try to see where the leak is located. For all the leaky instances, I can see the following (I...
4
6090
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password"; scope.Path.Path=@"\\pc\root\cimv2";
20
8115
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
1
1179
by: astroboy | last post by:
How do I detect which object cause memory leak???? Thx
23
4560
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
7
15713
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a lot of memory but I still had to use it. 1. After using DLLImport and seeing the memory leak I tried to load and
22
2205
by: Simon | last post by:
Hi all, I have a huge memory leak problem in what is really very simple data insert code. In my app I'm trying to use a typed dataset to insert into a database. It adds quite a few rows (say hundreds). Unfortunately it has a big ass memory leak and I dont understand why. The code below demonstrates the memory leak. It's like nothing is
6
5573
by: nmehring | last post by:
I have an MFC app with 2000 users. I have one user that experiences a crash in our software anywhere from 1 to 5 times a week when opening a particular module. No other users have reported this particular crash so I don't think anyone else is experiencing it but I know other users are doing exactly what she is doing because it is our most popular module. I have analyzed the dmp files from several of this user's crashes using windbg...
0
8957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9321
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8194
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6745
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4819
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3268
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.