Strange Error: heap corruption?...

Hey Girls... I got the following code...

when the method finishes the compiler shows me some error message, telling me that windows has executed an breakpoint or something like that(sry for the bad translation)...
when i click on "continue" a few times it resolves to an "Debug Assertion Failed"-Error with something about "Expression: _CrtIsValidHeapPointer(pUserData)"...

any clues?...

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
long CUserManager::GetNewID() const
{
	fstream file;
	//Comparison IDs
	long last, next;
	long* lpIDs;
	long* lpIDb;
	long arrsize = 0;
	long ID;
	char c;		//Check

	//Exclusive Access
	WaitForSingleObject(hFileMutex,INFINITE);
	//Open
	file.open(UserDataFileName, ios::app | ios::in);
	if(!file)
		return -1;

	while(file.good())
	{
		c = file.get();
		//cout << c;
		if(c == '\n')
			arrsize++;
	}
	//file status !good()
	//clear file status
	file.clear();


	//If One ID min
	//Set Array Size
	file.seekg(0,ios::end);
	streampos pos = file.tellg();

	if((int)pos == 0)
		return 1;

	lpIDs = new long((arrsize+1)*sizeof(long));


	//Fill Array With IDs
	long check = 0;
	long number;

	file.seekg(0,ios::beg);

	//Get First Number
	file >> number;
	lpIDs[check] = number;
	check++;

	while(file.good())
	{
		c = file.get();
		if(c == '\n')
		{
			file >> number;
			lpIDs[check] = number;
			check++;
		}
	}
	//file status !good()
	//clear status
	file.clear();
	file.close();

#ifdef _MYDEBUG_
	for(long l = 0; l < check; l++)
		cout << lpIDs[l];
#endif

	//Check for gaps

	for(long l = 0; l < check-1 ; l++)
	{
		last = lpIDs[l];
		next = lpIDs[l+1];
		if(next-last == 1)
			continue;
		else if(next-last > 1)
		{
			number = last+1;
			bool found = false;
			//Does That Number Exist Somewhere Else?
			for(long m = 0; m < check; m++)
			{
				if(lpIDs[m] == number)
				{
					found = true;
					break;
				}
			}
			if(!found)
			{
				delete [] lpIDs;
				return number;
			}
		}
	}

	delete [] lpIDs;
	//Return new ID
	return check+1;
}
Last edited on
Line 39 is wrong.
Consider difference between

new long( 4 );

and

new long[ 4 ];

thanks... may u explain me the difference, please?...


edit: am i right with the following?:

new long(4) -> allocates space for one long and initializes it with '4'; (or calls the constructor and passes 4 to it as an argument)

the new long[4] is clear... thought they were the same -.-'...
Last edited on
Yes. new long( 4 ) = allocate space for one long and intialize it with the value 4
new long[ 4 ] = allocate space for 4 uninitialized longs.

thank you
Topic archived. No new replies allowed.