Memory Leak issue??? (i think so anyways)

So, i'm working on a Binary Search Tree. The tree will eventually hold names of people in a family, but for we are required to use the BST for searching of people and what not.

Right now when ever I try to read root->name inside of my functions it is causing the following error:
Unhandled exception at 0x1048b28a in Project 4.exe: 0xC0000005: Access violation reading location 0x00000018.


Here is my code right now, i'm mostly concerned about the insert and print functions right now. All of the cout statements in the insert are just there to figure out where my problems are, and to read in the ft->name value as they are input. My only issue is that it reads in ft->name fine for the first person inserted, but won't read root->name


anyways heres the code:

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
#ifndef BST_H
#define BST_H



#include <fstream>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include "string"




using namespace std;


struct FTNode
{
       string name;
       FTNode *left;
       FTNode *right;
       };
       

class BinaryTree
{
public:
       BinaryTree();
       ~BinaryTree();
       bool isEmpty() const;
       void inorderTraversal() const;
       void inorder(FTNode *ft) const;
       void destroy(FTNode *ft);
       //void destroyFT(FTNode<FT> *ft);
       void getFamily(string& stuff, bool& found) const;         //slide 35
       void retrieve(FTNode *ft, string stuff, bool& found) const; //Slide 35/36
       void putitem(string stuff);
       void insert(FTNode *ft, string stuff);                // Slide 41        
       void getPredecessor(FTNode *ft, string& stuff) const;       // slide 49
       void print(std::ofstream& outfile) const;
       void printFT(FTNode* ft, std::ofstream& outFile) const; //slide 52
       void FindFT(FTNode* ft, string stuff, FTNode*& nodePtr, FTNode*& parentPtr) const; //slide 64
       
       
private:
        FTNode *root;   
};      

#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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "BST.h"



BinaryTree::BinaryTree()
//Constructor
//initalizes root of the tree
{
             root = new FTNode;
			 root = NULL;

}
             



BinaryTree::~BinaryTree() 
//Deconstructor
//Calls destory 
{
        destroy(root);
        }
        
       

bool BinaryTree::isEmpty() const
// Returns true if empty tree       
{
           return (root == NULL);
           }
           
           
           
void BinaryTree::inorderTraversal() const
// Default traversal method
{
           inorder(root);
           }
       
       
       
    
void BinaryTree::inorder(FTNode *ft) const
//inOrder Search
{       
       if(ft != NULL)
       {
             inorder(ft->left);
             cout << ft->name << " ";
             inorder(ft->right);
       }
}



void BinaryTree::destroy(FTNode *ft) 
{
     if(ft != NULL)
     {
           destroy(ft->left);
           destroy(ft->right);
           delete ft;
           ft = NULL;
     }
}       



/*template <class FT>
void BinaryTree<FT>::destroyFT(FTNode *ft);
// Destroy's the tree.   used for deconstructor
{
             destroy(root);
}
*/



void BinaryTree::getFamily(string& stuff, bool& found)    const     //slide 35
// GetInfo function.   calls retrieve function, searching for "stuff"
{
           retrieve(root, stuff, found);
           }





void BinaryTree::retrieve(FTNode *ft, string stuff, bool& found) const //Slide 35/36
// finds a specific item in the root node
{
         if(ft = NULL)
                 found = false;
         else if(stuff < ft->name) 
             retrieve(ft->left, stuff, found);
         else if(stuff > ft->name)    
             retrieve(ft->right, stuff, found);
         else
         {
             stuff = ft->name;
             found = true;
         }
}         

             

void BinaryTree::putitem(string stuff) 
{
     insert(root, stuff);
     }



/**************************/
//NOT GETTING STORED IN ROOT
//DON'T KNOW WHY
// 11/27/12 @ 1:06 AM
/**************************/


void BinaryTree::insert(FTNode *ft, string stuff)               // Slide 41        
{    
     /*FTNode* temp;
     temp = root;*/
	int response = 0;
	cout << "checker" << endl;
	/*if (root == NULL)
	{
		cout << "Root = Null called" << endl;
		root->name = stuff;
		cout << "root->name = " << root->name; << endl;
		root->left = NULL;
		cout << "left linked to null" << endl;
		root->right = NULL;
		cout << "right linked to null" << endl;
	}
	else*/ if (ft == NULL)
     {
		    cout << stuff << endl;
			cout << "Null called.   Continue?";
			cin >> response;
			ft = new FTNode;
			//cout << "New Node Made, continue?";
			//cin >> response;
            ft->right = NULL;
            ft->left = NULL;
			ft->name = stuff;
			cout << ft->name << endl;
            }
     else if(stuff < ft->name)
	 {
			cout << "left called" << endl;
            insert(ft->left, stuff);
	}
     else
	 {
			cout << "right called" << endl;
            insert(ft->right, stuff);
	 }

    cout << "2nd statement of name: " << ft->name << endl; 
	cout << "Root is: " << root->name << endl;
}            





void BinaryTree::getPredecessor(FTNode *ft, string& stuff) const       // slide 49
{
     while(ft->right != NULL)
     {
          ft->right;
          }
     stuff = ft->name;
     
     
}






void BinaryTree::print(std::ofstream& outfile) const
{
    FTNode* current;
	current = root;
	printFT(current, outfile);
     }





void BinaryTree::printFT(FTNode* ft, std::ofstream& outFile) const //slide 52
{
     if (ft != NULL)
     {
            printFT(ft->left, outFile);
            outFile << ft->name << "\n";
            printFT(ft->right, outFile);
     }
     
}






void BinaryTree::FindFT(FTNode* ft, string stuff, FTNode*& nodePtr, FTNode*& parentPtr) const //slide 64
{
     nodePtr = ft;
     parentPtr = NULL;
     bool found = false;
     while(nodePtr != NULL && !found)
     {
          if(stuff < nodePtr->name)
          {
                   parentPtr = nodePtr;
                   nodePtr = nodePtr->left;
                   }
          else if(stuff > nodePtr->name)
          {
               parentPtr = nodePtr;
               nodePtr = nodePtr->right;
               }
          else
              found = true;
     }
}





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
// MAIN FILE IMPLEMENTATION
// V 1.0 WILL ONLY TEST THE BST, NOT THE FAMILY TREE FUNCTIONS


// Made by Terryn Fredrickson


#include <fstream>
#include <iostream>
#include "BST.h"
#include <cstring>
#include <string>



using namespace std;


int main()
{
    string aname;
    BinaryTree FamTree;
    ofstream outfile;
    outfile.open("output.txt");
	int test;
    cout << "Please provide 3 data points" << endl;
    for(int n = 0; n < 3; n++)
    {
            cout << "Please provide name #" << n+1 << ": ";
            cin >> aname;
			//cout << aname << endl;
            FamTree.putitem(aname);
            aname = "";
            }
    cout << "Printing Family Tree data:\n";
	cin >> test;
    FamTree.print(outfile);
	cout << "Continue?";
		cin >> test;
                
        
    
    
    
    
return 0;    
}



edited: to clean up code in the header
Last edited on
In BinaryTree::insert, ft s a copy of the pointer value fed to it. Changes made to ft will be lost when the function returns.

Also,
1
2
3
4
5
6
7
8
BinaryTree::BinaryTree()
//Constructor
//initalizes root of the tree
{
             root = new FTNode;
			 root = NULL;

}


results in a memory leak.
Last edited on
Solutions have been found after pounding head into VS for the past few hours lol.


Memory leak was solved by:

fixing the constructor

1
2
3
4
5
6
7
8
9
10
11
BinaryTree::BinaryTree()
//Constructor
//initalizes root of the tree
{
             root = new FTNode;
			 root->name = "";
			 root->left = NULL;
			 root->right = NULL;

}
           




Other errors that were coming up were because my insert function was not working as it was supposed to. With the linking to the right or left, I needed to actually set up the link before running the recursive insert statement.

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
void BinaryTree::putitem(string stuff) 
{
     insert(root, stuff);
     }




void BinaryTree::insert(FTNode *ft, string stuff)               // Slide 41        
{    
	int response = 0;  // allows response to continue after first node made
	cout << "checker" << endl;
	
	if (ft->name == "")
     {
		    cout << stuff << endl;
			cout << "Null called.   Continue?";
			cin >> response;
			root = ft = new FTNode; // This is a new tree, so we need to initialize the root node.
			//cout << "New Node Made, continue?";
			//cin >> response;
            root->right = NULL;
            root->left = NULL;
			root->name = stuff;
			cout << root->name << endl;
            }
     else if(stuff < ft->name)
	 {
			cout << "left called" << endl;
            if (ft->left != NULL)
            {
                insert(ft->left, stuff);
            }
            else
            {
                ft->left = new FTNode();
                ft->left->name = stuff;
                ft->left->left = NULL;
                ft->left->right = NULL;
            }
	}
     else
	 {
			cout << "right called" << endl;
            if (ft->right != NULL)
            {
                insert(ft->right, stuff);
            }
            else
            {
                ft->right = new FTNode();
                ft->right->name = stuff;
                ft->right->left = NULL;
                ft->right->right = NULL;
            }
	 } 
	cout << "Root is: " << root->name << endl;
}            
Topic archived. No new replies allowed.