Memory Leak?

I have a vector of dynamically allocated objects. Will a vector's erase function free up the memory of a dynamically allocated object? So if I used the vectors erase function, I would not need to use the delete operator on that index? Will TreeFromListConstruction.cpp need to use the delete operator at all?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//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];
}


1
2
3
4
//TreeFromListConstruction.cpp
...
myList->erase(myList->begin() + leastIndex ); //remove least node from list
...
you'll need to delete the dynamically allocated objects yourself, or use auto_ptr in <memory>, which acts as a pointer except it deletes what it's pointing to when itself is deleted.
Thanks, would you happen to know any good articles on auto_ptr?
well, there's a page on this very site: http://cplusplus.com/reference/std/memory/auto_ptr/
it's used just like a regular pointer, you have to declare what it's pointing to at its construction, but you don't use delete.
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <memory>
using namespace std;
int main() {
	auto_ptr<int> p(new int);
	*p = 100;
	cout << *p;
        return 0;
}
Last edited on
iirc you can't use auto_ptr for this because it doesn't allow itself to be copied (doesn't reference count). In fact I think you get compiler errors if you try it (or at least I did when I tried it).

There's something called shared_ptr which does this, but it's nonstandard (at least not yet?). Plus I think there's something like that in Boost, too.

I ended up just writing my own, but that's not something I'd recommend.
bah, right. I was able to get away with the no copying with VC++, but they initialize to null and I don't think you can change what they point to. You could make your own smart pointer class, but it's probably easiest and best coding to just delete them yourself.
Alternatively boost provides the ptr_container library which defines a ptr_vector class that works identically to vector except that it does the delete for you when the element is removed.
Alright, I ran into quite a few problems regarding memory that I have questions about. First of all on this subject I think I'm going to try to take the delete them myself approach. I have already attempted to do this and now the program is blowing up during run time. I have an idea where it is blowing up at but I am not sure why. But first let me post the way I tried to delete it.



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
...




Can someone tell me if this is correct? I simply want to remove the element cleanly from the vector. Do I need to do the delete operator and the erase function to remove it from the vector?? If the problem is not seen then I think I will have to post my complete code and explain what I'm trying to do further so I can get help with getting my memory leaks resolved.


Still have a couple questions about memory leaks beyond this so stay tuned to this thread please ^_^
Last edited on
This probably isn't it, but maybe it's getting confused with the double subscript operators, trying to use them both myList. Might be better as:
delete (*myList)[leastIndex]
or the ugly
delete myList->operator[](leastIndex)
Last edited on
here's what I do:

to wipe the whole vector clean (be sure you do this rather than relying on vector's dtor to clear it):
1
2
3
4
5
while(!vec.empty())
{
  delete vec.back();
  vec.pop_back();
}


to remove/erase individual element with index 'i':
1
2
delete vec[i];
vec.erase(vec.begin() + i);
looks correct to me. Is there definitely a memory leak?
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?


Last edited on
Topic archived. No new replies allowed.