Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

beginner, have problem with memory leak

Status
Not open for further replies.

ssdave

Programmer
Jul 8, 2002
9
0
0
US
I am a beginner with visual C++ and am have a really bad problem with a memory leak. Our app has a dialog with a loop in it that checks a device and reads in a text file. I suspect that the variable which is having the text file inputted into it is accruing the data with every cycle. Looking at the performance tab in task manager, total memory use increase by 500k on every loop. I want the loop to dump all its variables at the end of each cycle and start over, particulary "mb".

#include "FPMemBase.h"
CFPMemBase mb;
mb.LoadFromFile("c:\\file\\default.ndb"); //lines from
//cpp



// CFPMemBase window //lines from
//FPMemBase.h
class CFPMemBase : public CWnd
{
// Construction
public:
CFPMemBase();

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFPMemBase)
//}}AFX_VIRTUAL

// Implementation
public:
// Removes all from the database
HRESULT RemoveAll();
// Makes a copy of database file into the memory
HRESULT LoadFromFile(CHAR * FileName);


virtual ~CFPMemBase();


I tried using RemoveAll() against mb, and it was undeclared.
Is there possibly any function I could call that would just purge all virtual memory or a list of specified variables?
Any help would be greatly appreciated.
 
well the way you should free all unused memory, depends on how you reserved the memory previously..... did you use malloc? use free..... did you use new? use delete.....
 
If you insert a bunch of pointers into your list and just call remove all, it will empty the list but leave your pointers intact. What you need to do is...

POSITION p;
p = list.GetHeadPostion();

while(p)
{
dataType* = list.GetNext(p);
delete dataType;
list.RemoveHead();
p=list.GetHeadPosition();
}


// the second call to get head postion is required.
You cant just assume that the head positon will always remain the same after you delete the head of the list.


Matt
 
Matt,
what header file includes RemoveHead(), GetHeadPosition()?

Thanks again for the help,

ssdave
 
Oh, I assumed with the &quot;RemoveAll&quot; call was in relation to CList<type,type> This is an MFC linked list which you can add to and delete from. When you get the memory leaks, the dump in debug should point you to where the actual problem is but you will have to open the file and goto that line number. Most likely you will see either a new or a malloc... Probabally a new because the malloc mem leaks are tougher to find. Go to that line and see what exactly your doing when you declare the pointer. If you never delete the pointer and just keep setting it to a new type then you will have mem leaks galore.

A simple check like:

if(ptr)
delete ptr;

ptr = new type

Or using CList<type*,type*> list;

list.Add(new type);

and then applying what I mentioned above will do the trick.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top