Destructor randomly called.

I created two classes Student and List. And as soon as I try to add student obj to a list it calls destructor. I somewhere read that it might be because it can't allocate memory, but maybe it is not the issue this time. I will post only relavent code.


Student header file:
1
2
3
4
5
6
7
8
9
10
  class Student{
public:
	Student();
	~Student();


private:
	List<unsigned short int> _grades = List<unsigned short int>(3);

};

Student cpp file:
1
2
3
4
5
6
7
8
9
10
#include "Student.h"

Student::Student(){
}

Student::~Student(){
	std::cout << "Destructor is called.";
	//_grades.~List(); // ja izkomentee nav erroru
}


list header file:
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
template <class type>
class List{
	public:
		List(unsigned int sizeToReserve = 10);
		~List();

		List<type>* add(type element);
		
	private:
		type* _list;
		unsigned int _reservedSize;
		unsigned int _occupiedSize;


		void _doubleListSize();
		void _resizeList(unsigned int size);
		void _copyList(type src[], unsigned int srcBegin, type dest[], unsigned int destBegin, unsigned int totalElements);
		void _deleteList();

		void _addElementAtEnd(type element);
		

};



And list cpp file:
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
#include "list.h"


template <class type>
List<type>::List(unsigned int sizeToReserve = 10){
	sizeToReserve = sizeToReserve > 0 ? sizeToReserve : 1;
	_list = new type[sizeToReserve];
	_reservedSize = sizeToReserve;
	_occupiedSize = 0;
}

template <class type>
List<type>::~List(){
	_deleteList();
}

template <class type>
void List<type>::_deleteList(){
	delete[] _list;
}

template <class type>
List<type>* List<type>::add(type element){
	//if (_currentListIsFull()){ _doubleListSize(); }

	_addElementAtEnd(element);
	return this;
}


template <class type>
void List<type>::_resizeList(unsigned int size){
	type* newList = new type[size];
	_copyList(_list, 0, newList, 0, _occupiedSize);
	_deleteList();
	_list = newList;
	_reservedSize = size;
}

template <class type>
void List<type>::_copyList(type src[], unsigned int srcBegin,type dest[], unsigned int destBegin, unsigned int totalElements){
	for (unsigned int index = 0; index < totalElements; index++){
		dest[index + destBegin] = src[index + srcBegin];
	}
}



template <class type>
void List<type>::_addElementAtEnd(type element){
	_list[_occupiedSize] = element;
	_occupiedSize++;
       //this is where Student destructor is called, I even tried to put return;
}


main cpp file:
1
2
3
4
5
int main(){
	List<Student> studentList = List<Student>();
	studentList.add(Student());
	return 0;
}

The unnamed temporary object (argument to studentList.add in line 3 of main) is destroyed when the function returns. This happens after that temporary object has been copied (copy-assigned) in List<>::_addElementAtEnd(), so this is as it should be.

List is a template; move the contents of list.cpp into the header (and throw list.cpp away).
See: 'Why can’t I separate the definition of my templates class from its declaration and put it inside a .cpp file?' http://isocpp.org/wiki/faq/templates
I have third header file with contains list.h and list.cpp. When I do that everthing works ok. Is it okey to do this way? Then I can keep definition separate from implementation.

And the issue is not about temp object Student(), but when main function ends(at return statement) program crashes with BLOCK_TYPE_IS_VALID(pHead->nBlockUse) line 52. I googled it and it occures when something is tried to delete multiple times.

Probably title of this thread is incorrect.

Big thanks to your assistance.
> I have third header file with contains list.h and list.cpp. When I do that everthing works ok. Is it okey to do this way?

Yes. Ideally do not call the second file list.cpp; instead use a name like list.inl


> when main function ends(at return statement) program crashes

You need to write a proper copy constructor and an overloaded assignment operator for the class List<> (deep copy is required).
(Class Student has the member _grades which is a list.)
Topic archived. No new replies allowed.