destructor is randomly called

Jun 26, 2011 at 4:50am
I want to thank all who will help me ahead of time.

I wrote a ADT liked list using template.
when I tested it with primitive data types, such as int and float, it successfuly appended elements onto the list, and the destructor was automatically called after as it should be. However when I used it to store classes, when the append function is called, it jumps out of the function without appending the class object and goes straight to the destructor(I am sure of this because I have used break points, watches and also it thrown an empty list exception when it was calling the remove all function)

I have posted my code below, it contains both approaches of using struct and using class node type. it currently is using struct approach of making the nodes

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
//File: List.h
//Author: Mwang


#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_
#include <iostream>		// For cout and NULL
using namespace std;

template <class Type>
class ListNode{
public:
	Type value;
	ListNode<Type> *next;

	ListNode(){
		next = NULL;
	};
	ListNode(Type nodeValue){
		value = nodeValue;
		next = NULL;
	}
	~ListNode(){}
};

template <class Type>
class LinkedList{
private:
	//ListNode<Type> *head;
	struct ListNode{
		Type value;	// Value in this node
		ListNode *next;		// Point to the next node
	};
	ListNode *head;			// List head pointer
	int numElement;
	
public:
	// Constructor
	LinkedList(){
		head = NULL;
		numElement = 0;
	}

	// Destructor
	~LinkedList();

	// Linked list operations
	int size();
	void add(Type element);
	Type remove(Type element);
	void removeAll();
	Type get(Type element);

	// Exception class
	class duplicateElementException{}; //enmpty class declaration
	class emptyListException{}; //enmpty class declaration
	class notFoundException{};
	
	// Test function
	void displayList();
};

template <class Type>
int LinkedList<Type>::size(){
	return numElement;
}

template <class Type>
void LinkedList<Type>::add(Type element){
	//ListNode<Type> *newNode; // Point to a new node
	//ListNode<Type> *nodePtr; // Move thru the list
	ListNode *newNode; // Point to a new node
	ListNode *nodePtr; // Move thru the list
	
	// Allocate a new node & store element there.
	/*newNode = new ListNode<Type>(element);*/

	newNode = new ListNode;
	newNode->value = element;
	newNode->next = NULL;

	// If no nodes in list, make newNode first node.
	if (!head){
		head = newNode;
		numElement += 1;	// Add 1 to size counter
	}else { // otherwise, insert newNode at end.
		// Initialize nodePtr to head of list.
		nodePtr = head;
		// Find last node in list
		while (nodePtr->next){
			// Duplicate check
			if (nodePtr->value == newNode->value){
				throw duplicateElementException();
			}
			nodePtr = nodePtr->next;
		}
		// Duplicate check for list contain only 1 element
		if (nodePtr->value == newNode->value){
				throw duplicateElementException();
		}
		// Insert newNode as last node.
		nodePtr->next = newNode;
		numElement += 1;	// Add 1 the element counter
	}
}

template <class Type>
LinkedList<Type>::~LinkedList(){
	cout <<"Destructor is called.";
}
#endif LINKEDLIST_H_ 
Last edited on Jun 27, 2011 at 6:01am
Jun 26, 2011 at 4:55am
I'll need to see the code.
Jun 26, 2011 at 5:35am
the problem persists, the destructor is now called right after the add method is finished. I can't pin point exactly which line causes this problem because every once in a while the destructor is called at a random point in the program.
Jun 26, 2011 at 6:24am
Can you give me the relevant part of your main function? I ran it with the following:

1
2
3
4
5
6
int main() {
	LinkedList<int> test;
	test.add(10);
	std::cin.get();
	return 0;
}


And it did not print "Destructor is called" until after I hit enter.
Jun 26, 2011 at 6:43am
Thank you very much

I can across an article, saying something like reserve some initial space. could it be that the it does not have enough memory reserve to do the operation, therefore it calls the destructor early to free memory? And also i was researching on the web can came across a definition of the destructor as "A destructor is called for a class object when that object passes out of scope or is explicitly deleted." could it be that the program was going out of scope? dont exactly understand.

below are the more detailed codes that the problem occurs at

1
2
3
4
5
6
int main(){
	CustDB db;
	db.addCustomer("M","W");
	cin.get();
	return 0;
}


and the member function of that class, which contains a list called customerList

1
2
3
4
5
void CustDB::addCustomer(string setFirstName, string setLastName){
	Customer customer(setFirstName, setLastName);
	customerList.add(customer);
	numOfCustomer++;
}

the destructor is called twice, at the end of the call at line 2 and line 3 of above code.

If its not too much to ask, is it possible for me to just send the entire solution to you by email, b/c I have quite a few headers and cpp files for classes.
Last edited on Jun 26, 2011 at 7:04am
Topic archived. No new replies allowed.