Memory leak problem. C++ Task Manager!

Been sick the last week, haven't had a lot of time to work on my program..Due tomorrow for class.My problem is that for some reason my program is creating errors when I add the "memory leak function". I have the correct headers, it just breaks when I run the program.

/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

My other problem is that I have to make sure my program follows these guidelines...Please keep in mind that my program is split into multiple .cpp and .h files, not all the program is here but the main bit where I think the error originates.

1
2
3
4
5
6
7
8
9

    Use structs or class namedTaskto model task. You really should think about changing the struct to class if you have not done so in Lab 2.
    Use class namedTaskListto model the collection of tasks.
    Usedynamicarray ofTaskto implementTaskList.
    Usedynamiccharacter array to model the strings inTask,such as course name, task description and due date. The character array should be the exact size as needed, e.g "CS162" should use an charcter array of size 6 including '\0'.
    Usedestructorto deallocate the dynamic memory for the object.
    Make sure your program is "memory-leak-free" by using thememory leak detection tool. Please view the video in the module for detailed instructions about how to enable the tool in your program.
    When using class, please make sure you encapsulate the data which means make all the instance data memberprivateand provide accessor methods and mutator methods to access and manipulate the data.
    You are required to separate the specification from the implementation using .h and .cpp files.


This is the code that deals with the "guidelines" I should be following..

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
#include "TasksList.h"
#include <iostream>
using namespace std;

TaskList::TaskList()
{
	name = new char[strlen("no name")+1];
	strcpy(name, "no name");
	description = new char[strlen("no description")+1];
	strcpy(description, "no description");
	duedate = new char[strlen("no duedate")+1];
	strcpy(duedate, "no duedate");
}

TaskList::TaskList(const char name[], const char description[], const char duedate[])
{
	this->name = new char[strlen(name)+1];
	strcpy(this->name, name);	
	this->description = new char[strlen(description)+1];
	strcpy(this->description, description);
	this->duedate = new char[strlen(duedate)+1];
	strcpy(this->duedate, duedate); 
}

TaskList::~TaskList()
{
	if(name != NULL)
		delete [] name;
	if(description != NULL)
		delete [] description;
	if(duedate != NULL)
		delete [] duedate;
}


void TaskList::getName(char name[]) const
{
	strcpy(name, this->name);
}

void TaskList::getDescription(char description[]) const
{
	strcpy(description, this->description);
}

void TaskList::getDuedate(char duedate[]) const
{
	strcpy(duedate, this->duedate);
}

void TaskList::print() const
{
	cout << name << " -> " << description << " -> " << duedate << endl;
}

void TaskList::setName(const char name[])
{
	if(this->name != NULL)
		delete [] this->name;
	this->name = new char[strlen(name)+1];
	strcpy(this->name, name);
}

void TaskList::setDescription(const char description[])
{
	if(this->description != NULL)
		delete [] this->description;
	this->description = new char[strlen(description)+1];
	strcpy(this->description, description);
}

void TaskList::setDuedate(const char duedate[])
{
	if(this->duedate != NULL)
		delete [] this->name;
	this->duedate = new char[strlen(duedate)+1];
	strcpy(this->duedate, duedate);
}


Then this is the code where I think the "Memory leak error is coming up from"..

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
//memory leak detection tool headers
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#include <iostream>                 
#include <cctype>

#include "TasksList.h"
#include "EntryList.h"
#include "InputTools.h"

using namespace std;     

void displayMenu();
char readInCommand();
void processCommand(char command, Tasks& list);
void readInEntry(TaskList& anEntry);
void readInName(char name[]);

int main ()
{

	char			command;
	char			fileName[] = "tasks.txt";
	Tasks		list(1, fileName);

	displayMenu();
	command = readInCommand();
	while (command != 'q')
	{
		processCommand(command, list);
		displayMenu();
		command = readInCommand();
	}

	list.saveTasksBook(fileName);	
    
    return 0;

}

void displayMenu()
{
	cout << endl << "Hello User! Welcome to the TaskManager" << endl << endl;
	cout << "\ta: To add an entry" << endl
		<< "\tl: To list all the entries" << endl
		<< "\ts: To search class by name" << endl
		<< "\tq: To exit this program" << endl << endl;
}

char readInCommand()
{
	char	cmd;

	cmd = readChar("Please enter the command (a,l,s or q): ");

	return tolower(cmd);
}

void processCommand(char command, Tasks& list)
{
	TaskList	entry;
	char			name[MAX_CHAR];
	char			description[MAX_CHAR];
	char			duedate[MAX_CHAR];

	switch (command)
	{
	case 'a': readInEntry(entry);
		list.addTasks(entry);
			break;

	case 'l': list.printAll();
			break;

	case 's': readInName(name);
			  if(list.searchTasks(name, entry))
			  {
				  entry.getDescription(description);
				  entry.getDuedate(duedate);
				  cout << endl << "The class for " << name << ": " << description << ": " << duedate << endl;
			  }
			  else
				  cout << endl << "The class for " << name << " doesn't exist." << endl;
			break;

	default: cout << endl << "Illegal Command!" << endl;
			break;
	}
}	

void readInEntry(TaskList& anEntry)
{
	char	name[MAX_CHAR];
	char	description[MAX_CHAR];
	char	duedate[MAX_CHAR];


	readString("Please enter the name: ", name, MAX_CHAR);
	readString("Please enter the description: ", description, MAX_CHAR);
	readString("Please enter the duedate: ", duedate, MAX_CHAR);


	anEntry.setName(name);
	anEntry.setDescription(description);
	anEntry.setDuedate(duedate);
}

void readInName(char name[])
{
	readString("Please enter the name you want to search: ", name, MAX_CHAR);
}


Thanks for all the help in advance! :)

http://www.cplusplus.com/forum/general/112111/
http://www.eelis.net/iso-c++/testcase.xhtml

> Use structs or class named Task to model task
> Use class named TaskList to model the collection of tasks.
Your `TaskList' code seems more appropriate to `Task'

> Use dynamic character array to model the strings in Task
I suggest you to create a class that encapsulates that behaviour
Given that you have a non-trivial destructor for `TaskList', you'll also need to code a proper copy constructor and an assignment operator. You should move that logic to the `dynamic_character' class.


PS: ¿what's your issue with whitespace?
Topic archived. No new replies allowed.