472,353 Members | 1,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 2184

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.public.* newsgroups or
comp.unix.programmer, 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
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...
17
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...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope();...
20
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...
1
by: astroboy | last post by:
How do I detect which object cause memory leak???? Thx
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; ...
7
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...
22
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...
6
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.