question about _CrtDumpMemoryLeaks( )
Mar 31, 2009 at 6:53am UTC
I have a class that can load a file and put the contents of it into a vector. Its contructor takes in a vector<char> by reference. I think it may be leaking memory according to _CrtDumpMemoryLeaks( ). The way I think that function works is when it is called it returns a 1 if there are any memory leaks present in the program at that time, is that correct? Let me post the code, can anyone see a memory leak in this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//main.cpp
...
int main()
{
vector<char > vFile;
vector<char > charByte;
vector<int > frequency;
vector<Node*> LeafNodeList;
cout << _CrtDumpMemoryLeaks( ); //returns 0
FileManager File("TestFile.txt" );
cout << _CrtDumpMemoryLeaks( ); //returns 0
File.loadFileIntoVector(&vFile);
cout << _CrtDumpMemoryLeaks( ); //returns 1
...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
//FileManager.h
#ifndef FILEMANAGER_H
#define FILEMANAGER_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class FileManager
{
public :
FileManager(string fName);
~FileManager(){;}
void loadFileIntoVector(vector<char > *file);
private :
string fileName;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//FileManager.cpp
#include "FileManager.h"
FileManager::FileManager(string fName)
{
fileName = fName;
}
void FileManager::loadFileIntoVector(vector<char > *file)
{
ifstream fin;
fin.open(fileName.c_str(), ios::binary);
if (!fin)
cout << "unable to open file\n" ;
char ch;
while (fin.get(ch))
file->push_back(ch);
fin.close();
}
Last edited on Mar 31, 2009 at 6:54am UTC
Mar 31, 2009 at 12:11pm UTC
You should place _CrtDumpMemoryLeaks() at the end of your program. It doesn't make much sense to check the heap for leaks while it's still in use.
Topic archived. No new replies allowed.