When I do this:
1 2 3 4 5
|
//TreeFromListConstruction.cpp
...
delete myList[0][leastIndex];//new line I added in attempt to clean up memory leak
myList->erase(myList->begin() + leastIndex ); //remove least node from list
...
| |
During run time my program comes up with a dialog box saying:
"Windows has triggered a breakpoint in Huffman.exe.
This may be due to a corruption of the heap, which indicates a bug in Huffman.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Huffman.exe has focus.
The output window may have more diagnostic information."
I used breakpoints and It appears it does not blow up after this line:
delete myList[0][leastIndex];
but it appears that it is side effecting another thing that I'm trying to do in my code.
Also Gumber I tried each of the above two lines that you suggested and it still blow up, but I think I know why. I'm going to go ahead and post the entire TreeFromListConstruction class where the problem occures.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
//TreeFromListConstruction.h
#ifndef TREEFROMLISTCONSTRUCTION_H
#define TREEFROMLISTCONSTRUCTION_H
#include "LeafNode.h"
class TreeFromListConstruction
{
public:
TreeFromListConstruction(vector<Node*> *aList);
~TreeFromListConstruction();
void formTree();
private:
vector<Node*> *myList;
vector<Node*> lowestTwoFreqs;
vector<char> lowestTwoChars;
};
#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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
//TreeFromListConstruction.cpp
#include "TreeFromListConstruction.h"
TreeFromListConstruction::TreeFromListConstruction(vector<Node*> *aList)
{
myList = aList;
}
void TreeFromListConstruction::formTree()
{
// set least to first element
int leastFrequency = (myList[0][0])->getFrequencyNumber();
// int leastFreqChar = (myList[0][0])->getCharacterByte(); //also store character associated with frequency
int leastIndex = 0;
int loopTwice = 0;
int loopN = 0;
while(myList->size() > 1)
{
while(loopTwice < 2) //loop twice to get lowest two
{
for(unsigned int i = 0; i < myList->size(); i++)
{
if((myList[0][i])->getFrequencyNumber() < leastFrequency)//if we find a frequency in the list lower then our current least then set it to least
{
leastIndex = i;
leastFrequency = (myList[0][i])->getFrequencyNumber();
}
}
//cout << "The least frequncy is: " << leastFrequency << endl;
lowestTwoFreqs.push_back(myList[0][leastIndex]); //we need to save this node before removing it from list
delete myList[0][leastIndex];
myList->erase(myList->begin() + leastIndex ); //remove least node from list
if(myList->size() > 0)
{
leastFrequency = (myList[0][0])->getFrequencyNumber();//initialize a few of these guys back
}
// leastFreqChar = (myList[0][0])->getCharacterByte(); //also store character associated with frequency
loopTwice++;
leastIndex = 0;
}
myList->push_back(new Node(lowestTwoFreqs[0], lowestTwoFreqs[1]));
loopN++;
//for(unsigned int i = 0; i < lowestTwoFreqs.size(); i++)//destroy list of lowestFreqs
//{
// delete lowestTwoFreqs[i];
//}
lowestTwoFreqs.clear();
loopTwice = 0;
}
(myList[0][0])->clearBitVector(); //clear the root node's bit vector so it will not contribute to the leaf node bit assignment
}
| |
and remember that the related main function code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
//main.cpp
...
vector<Node*> LeafNodeList;
...
for(unsigned int i = 0; i < charByte.size(); i++)//putting the data into a LeafNode and the LeafNode into a list
{
LeafNodeList.push_back(new LeafNode(charByte[i], frequency[i]));
}
...
TreeFromListConstruction tree(&LeafNodeList);
...
for(unsigned int i = 0; i < LeafNodeList.size(); i++)//destroy list of LeafNodes
{
delete LeafNodeList[i];
}
cout << _CrtDumpMemoryLeaks( );
system("pause");
return 0;
| |
I used this line
cout << _CrtDumpMemoryLeaks( );
I believe it is supposed return a 1 if memory leaks are in the program? Which it does output a 1.
My theory is that when I do this line:
delete myList[0][leastIndex];
in the void TreeFromListConstruction::formTree() function it also effects this line
lowestTwoFreqs.push_back(myList[0][leastIndex]);
which I do earlier one line up and it messes up what was pushed into lowestTwoFreqs vector and it eventually blows up on after executing this line:
myList->push_back(new Node(lowestTwoFreqs[0], lowestTwoFreqs[1]));
a few times
Well that's my theory... I kind of coded deep into this thinking I was good to go and then I realized that shoot I think I got some major memory leak issues going on here, I'm kind of new with playing with dynamically allocated objects and such but I really want to fix this and do it right. Please look over the code and if you have any questions or need some more of the code posted let me know but can you see why this is blowing up and how I can fix it?