Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-16-2007, 02:39 PM   #1 (permalink)
complete
Code Monkey
 
complete's Avatar
 
Join Date: Jul 2005
Location: St. Louis
Posts: 76
complete is on a distinguished road
Looking for Memory Leak Advice

Apart from buying BoundsChecker or any of the other tools to track down memory leaks. Does anyone have any advice or tips from your years of programming experience to offer on how to track down the causes of memory leaks?

I find that the big dump that happens at the end of a program does not typically lend any useful clues as to where or what was allocated that was not freed. I find that I have to back track and comment out sections of code until through the process of elimination and trial-and-error, I am able to narrow down where the problem is. And since this is usually at the bottom of the To-Do list, the memory leaks are not plugged.
__________________
46 45 52 4D 49 20 57 41 53 20 4A 45 44 49 2E 10
20 53 41 47 41 4E 20 57 41 53 20 53 49 54 48 2E
complete is offline   Reply With Quote
Old 10-17-2007, 09:11 AM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Now obviously I don't know what sort of allocation strategy your program uses or what's appropriate for your problem domain, but you could try making some of your objects reference counted. Boost::SmartPointer is pretty good.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-18-2007, 11:56 AM   #3 (permalink)
complete
Code Monkey
 
complete's Avatar
 
Join Date: Jul 2005
Location: St. Louis
Posts: 76
complete is on a distinguished road
Quote:
Originally Posted by teknomage1 View Post
Now obviously I don't know what sort of allocation strategy your program uses or what's appropriate for your problem domain, but you could try making some of your objects reference counted. Boost::SmartPointer is pretty good.
Using some sort of smart pointer is something I will try when all else fails.

The Memory Dump that happens at the end of running the program (It is a Visual C++ MFC program) shows memory locations. I understand I can use this to find where the offending allocation happens. But the memory addresses change each time I run the program -- althought the sizes do not, but this does not give me enough clues.

I wonder if this is just an unavoidable "feature" of MFC.
__________________
46 45 52 4D 49 20 57 41 53 20 4A 45 44 49 2E 10
20 53 41 47 41 4E 20 57 41 53 20 53 49 54 48 2E
complete is offline   Reply With Quote
Old 10-19-2007, 01:13 AM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 734
DJMaze is on a distinguished road
Totally depends who forgets to cleanup something?

When you use
Code:
CString *foo = new CString();
but forget to
Code:
if (foo) delete foo;
or use

Code:
int *integers = new int[20];
but forget to
Code:
delete [] integers;
Then it's your fault, else it could be from a library that you use (.NET fun anyone?).
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 10-23-2007, 10:05 AM   #5 (permalink)
complete
Code Monkey
 
complete's Avatar
 
Join Date: Jul 2005
Location: St. Louis
Posts: 76
complete is on a distinguished road
Also, I have learned that you need to have a
CoUnitialize()
to go along with every
CoInitailze(0)

I wonder if I need something to go along with my CoCreateInstance calls.
__________________
46 45 52 4D 49 20 57 41 53 20 4A 45 44 49 2E 10
20 53 41 47 41 4E 20 57 41 53 20 53 49 54 48 2E
complete is offline   Reply With Quote
Old 10-26-2007, 02:29 PM   #6 (permalink)
complete
Code Monkey
 
complete's Avatar
 
Join Date: Jul 2005
Location: St. Louis
Posts: 76
complete is on a distinguished road
Quote:
Originally Posted by DJMaze View Post
Totally depends who forgets to cleanup something?

When you use
Code:
CString *foo = new CString();
but forget to
Code:
if (foo) delete foo;
or use

Code:
int *integers = new int[20];
but forget to
Code:
delete [] integers;
Then it's your fault, else it could be from a library that you use (.NET fun anyone?).
If I have something like this:

Code:
  BSTR val;
  SomeFunction(&val);
Will I have to free val somehow?
__________________
46 45 52 4D 49 20 57 41 53 20 4A 45 44 49 2E 10
20 53 41 47 41 4E 20 57 41 53 20 53 49 54 48 2E
complete is offline   Reply With Quote
Old 08-22-2008, 05:34 PM   #7 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 75
landonmkelsey is on a distinguished road
memory leaks

as I remember the ways (some of them)

(1)
Code:
#include <iostream>
using namespace std;
class Base
{	int*	pBase;
public:
	Base(){pBase=new int[8];cout<<"Base construCTOR"<<endl;}
	virtual ~Base(){delete [] pBase; cout<<"Base destruCTOR"<<endl;}
};
class Derived : public Base
{	int*	pDerived;
	int	ipd;
public:
	Derived()
	{	pDerived=new int[4096];
		cout<<"Derived construCTOR pointer = "<<hex<<pDerived<<endl;
	}
	~Derived(){delete [] pDerived; cout<<"Derived destruCTOR"<<endl;}
};
void
main()
{	for(int i=0; i<4;i++)
	{	Base* pB = new Derived;
		delete pB;
	}
}
To make the program fail, remove the virtual from the base class DTOR.

This exampled demos the concept

If the base class DTOR is virtual, the derived class DTOR will be called and memory allocated by the derived class will be released.


(2) If one stores pointers (created by new) in a container, one should perform a delete on the pointers after extraction (and final use) from the container. Then the rest of the pointers still in the container should be extracted and deleted.

(3) (mentioned above)the concept of a smart pointer is good because when the smart pointer goes out of scope (it is on the stack), its destructor is called and the memory allocation pointer is deleted.

(4) a new operation creates a pointer to memory and the pointer is passed to other methods. The general question is "who's responsibility is it to delete the pointer"

I have a vector program where ownership is decided at insert time. If the "vector" owns the pointers, it deletes the ones stored and the vector dies by going out of scope.

The example deletes the pointer and then allocates another chunk. If the new pointer is beyond the original, there is a memory leak.

(5) hmmm! what else???

If in doubt, add some cout statements and do the bookkeeping!
(6) I'll not mention the programming system but once I would pass on pointers and never get the chance to delete the pointers.

Tech support said "don't worry" SHUHR!

Last edited by landonmkelsey; 08-22-2008 at 05:43 PM. Reason: saw some typos
landonmkelsey is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any advice on securing a wireless network? cheawick Lounge 11 05-25-2005 09:23 AM
Memory Leak Question wishknew Standard C, C++ 5 02-13-2005 05:38 PM
Yet another Memory Leak Question fp_unit Standard C, C++ 3 02-10-2005 12:28 PM
Gonna give Java a shot, any advice? cheawick Java 3 01-01-2005 10:00 AM
Revolution is starting Counter-Strike, advice for the n00b saline Lounge 13 06-13-2003 09:55 AM


All times are GMT -8. The time now is 01:36 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting