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, 01:39 PM   #1 (permalink)
complete
Code Monkey
 
complete's Avatar
 
Join Date: Jul 2005
Location: St. Louis
Posts: 71
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, 08:11 AM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
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, 10:56 AM   #3 (permalink)
complete
Code Monkey
 
complete's Avatar
 
Join Date: Jul 2005
Location: St. Louis
Posts: 71
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, 12:13 AM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 651
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, 09:05 AM   #5 (permalink)
complete
Code Monkey
 
complete's Avatar
 
Join Date: Jul 2005
Location: St. Louis
Posts: 71
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, 01:29 PM   #6 (permalink)
complete
Code Monkey
 
complete's Avatar
 
Join Date: Jul 2005
Location: St. Louis
Posts: 71
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
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 08:23 AM
Memory Leak Question wishknew Standard C, C++ 5 02-13-2005 04:38 PM
Yet another Memory Leak Question fp_unit Standard C, C++ 3 02-10-2005 11:28 AM
Gonna give Java a shot, any advice? cheawick Java 3 01-01-2005 09:00 AM
Revolution is starting Counter-Strike, advice for the n00b saline Lounge 13 06-13-2003 08:55 AM


All times are GMT -8. The time now is 05:05 AM.


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