Delete Pointer

Hello everybody,

I know this is an evergreen topic, but I openly admit, I am lost. I have the following 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
52
53
54
55
56
57
58
59
60
61
// read bond definitions from a file
void myBond::readBonds(string bondsFile, vector<bondInfo> * bonds)
{
	// define variables
    char * fileLine = new char[_LINE_SIZE];
	char * column = new char[_COLUMN_SIZE];
	ifstream myFile;
	bondInfo bond_aux;
	myDate perpetualBondMaturity;

	// file names 
	bondsFile = _CONTRACT_INFO_PATH + bondsFile + _DATA;

	// open file
	myFile.open (bondsFile.c_str(), fstream::in);
	if (!myFile.is_open())
	{
        writeTo(_LOG_FILE, "readBonds() - File '" + bondsFile + "' does not exist!");
        myPause();
		exit(1);
	}
	else
	{
		// skip headers
		myFile.getline(fileLine, _LINE_SIZE);
		// go line by line
		while (!myFile.eof())
		{
			// load new line
			myFile.getline(fileLine, _LINE_SIZE);

			// continue unless you encounter an empty line
			if(strcmp(fileLine, "") != 0)
			{
				// parent ID
				getColumn(fileLine, 1, _DELIMITER, column);
				bond_aux.parentId = (strlen(column) == 0 ? 0 : atoi(column));
				
				...

				// floor for a floating bond
				getColumn(fileLine, 29, _DELIMITER, column);
				bond_aux.floor = (strlen(column) == 0 ? -1. : atof(column));


				// add bond to the vector
				(*bonds).push_back(bond_aux);
			}
		}
	}

	// close file
	myFile.close();
	myFile.clear();

	// delete pointers
    	delete [] fileLine;
	fileLine = NULL;
	delete [] column;
	column = NULL;
}


And sometimes (only for specific input files) for some reason (I have not discovered any pattern) I get an error message on line
 
delete [] column;

I wonder want might be wrong...

Thx a lot in advance for any suggestions.

Macky
If it's crashing when you delete, that probably means you have memory corruption. Check your array accesses and make sure you're not stepping out of bounds of any arrays.


For a more hands-on approach, set a breakpoint when you create 'column' and another when you delete[] it. Make sure the pointer is identical in both places (if it changed, that indicates that memory corruption is mucking up that pointer).

If that turns out to be the problem, step through the code, keeping an eye on that pointer and seeing which line changes it. Then once you know the line that's changing it you can see where the problem is.

Or if you're debugger supports it, you can set a memory breakpoint to break when 'column' changes, then that'll hopefully snap you exactly to the line that's the culprit.



HOW you do all this depends on your debugger/IDE. Though all the ones worth using support the basic functionality required for this:
- breakpoints
- step into / step over
- watch
Topic archived. No new replies allowed.