Memory Leak

Hi,

I'm having a memory leak problem with some code.
The program runs through a few thousand records and then crashes with a System.AccessViolationException - Attempted to read or write protected memory.
I've had a look through and I can't find anything.
However as its a memory leak it seems the debugger jumps to some random code, probably the memory address the leak has written.
As it runs through several thousand records, I've narrowed the code down to the code below.

Any help would be appreciated see code below:
The code starts in the checkforrecords() function at the bottom.
Edit: uRec.reasonText is a char[350] and uRec.filename is an unsigned char[200]
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
int writeUSN(UsnRec* uRec)
{
	SYSTEMTIME stUTC, stCurrent, stOld;
	FILETIME cur, fOld;
	char timeDisplay[30];
	//creat 2 timestamps, one 1 year in future and one from 1980
	GetSystemTime(&stCurrent);
	GetSystemTime(&stOld);
	stOld.wYear = 1980;
	stCurrent.wYear+=1;
	SystemTimeToFileTime(&stCurrent, &cur);
	SystemTimeToFileTime(&stOld, &fOld);
	if ((CompareFileTime(&cur,&uRec->timestamp) == 1) && (CompareFileTime(&fOld,&uRec->timestamp) == -1))
	{
		FileTimeToSystemTime(&uRec->timestamp,&stUTC);
		sprintf(timeDisplay,"%04d\\%02d\\%02d %02d:%02d:%02d\0",stUTC.wYear,stUTC.wMonth,stUTC.wDay,stUTC.wHour,stUTC.wMinute,stUTC.wSecond); 
	}
	else
	{
		return 0;
	}
	fprintf(fileUSN,"\"%s\",\"%s\",\"%s\",\"%llu\"\n", uRec->fileName, uRec->reasonText,timeDisplay,uRec->timestamp);
	return 1;
}

void reasonString(UsnRec* uRec)
{

	int check = uRec->reason & 0x000000FF;
	int check4 = (uRec->reason >> 8) & 0x000000FF;
	int check6 = (uRec->reason >> 16) & 0x000000FF;
	int check8 = (uRec->reason >> 24) & 0x000000FF;
	//all for bytes 7 - 8
	if (check8 & 0x80)
	{
		strncat(uRec->reasonText, "File Closed.\0", 250);
	}
	//all for bytes 5 - 6
	if (check6 & 0x04)
	{
		strncat(uRec->reasonText, "File Encryption Changed.\0", 250);
	}	
	if (check6 & 0x02)
	{
		strncat(uRec->reasonText, "File Compression Changed.\0", 250);
	}
	//all for bytes 3 - 4
	if (check4 & 0x80)
	{
		strncat(uRec->reasonText, "File/folder Attributes Changed.\0", 250);
	}
	if (check & 0x20)
	{
		strncat(uRec->reasonText, "File Renamed (New name).\0", 250);
	}
	if (check & 0x10)
	{
		strncat(uRec->reasonText, "File Renamed (Old name).\0", 250);
	}	
	if (check & 0x04)
	{
		strncat(uRec->reasonText, "Extended attributes changed.\0", 250);
	}
	if (check4 & 0x02)
	{
		strncat(uRec->reasonText, "File/folder Deleted.\0", 250);
	}
	if (check4 & 0x01)
	{
		strncat(uRec->reasonText, "File/folder Created.\0",250);
	}
	//all for bytes 1 - 2
	if (check & 0x20)
	{
		strncat(uRec->reasonText, "File Extended (Added to).\0", 250);
	}	
	if (check & 0x01)
	{
		strncat(uRec->reasonText, "File Overwritten.\0", 250);
	}
	if (check & 0x04)
	{
		strncat(uRec->reasonText, "File truncated.\0", 250);
	}
	if (uRec->reasonText[0] == '\0')
	{
		sprintf(uRec->reasonText, "Unknown action - %l \0", uRec->reason);
	}
}

int extractRecord()
{
	UsnRec uRec;
	unsigned char chrLen[4], chrMin[4], chrFileRef[8], chrParentRef[8],chrUSN[4], chrTimestamp[8],chrReason[4],chrSecurity[4],chrSource[4],temp[200];
	unsigned long long filePointer;
	filePointer = bufferPointer;
	memcpy(chrLen,(char*)&fileBuffer[filePointer], 4);
	uRec.recLen = *(DWORD*)&chrLen;
	if (uRec.recLen > 512)
	{
		//record too big.
		return 0;
	}
	filePointer += 4;
	uRec.majVer = fileBuffer[filePointer];
	filePointer += 4;
	memcpy(chrMin,(char*)&fileBuffer[filePointer], 4);
	uRec.minVer = *(DWORD*)&chrMin;
	filePointer += 4;
	memcpy(chrFileRef,(char*)&fileBuffer[filePointer], 8);
	uRec.FileRef = *(DWORDLONG*)&chrFileRef;
	filePointer += 8;
	memcpy(chrParentRef,(char*)&fileBuffer[filePointer], 8);
	uRec.ParentFileRef = *(DWORDLONG*)&chrParentRef;
	filePointer += 8;
	memcpy((char*)chrUSN,(char*)&fileBuffer[filePointer], 4);
	uRec.usn = *(USN*)&chrUSN;
	filePointer += 4;
	memcpy(chrTimestamp,(char*)&fileBuffer[filePointer], 8);
	uRec.timestamp = *(FILETIME*)&chrTimestamp;
	filePointer += 8;
	memcpy(chrReason,(char*)&fileBuffer[filePointer], 4);
	uRec.reason = *(DWORD*)&chrReason;
	filePointer += 4;
	memcpy(chrSource,(char*)&fileBuffer[filePointer], 4);
	uRec.sourceinfo = *(DWORD*)&chrSource;
	filePointer += 4;
	memcpy(chrSecurity,(char*)&fileBuffer[filePointer], 4);
	uRec.securityID = *(DWORD*)&chrSecurity;
	filePointer += 4;
	uRec.FileNameLength = fileBuffer[bufferPointer + 56];
	if (uRec.FileNameLength > 300)
	{
		//filename length too long
		return 0;
	}
	memcpy(temp,&fileBuffer[bufferPointer + 59], uRec.FileNameLength);
	for (int i=1; i <= uRec.FileNameLength; i+= 2)
	{
		uRec.fileName[i/2] = temp[i];
	}
	uRec.fileName[(uRec.FileNameLength/2)] = '\0'; 
	int lenCheck = 60 + uRec.FileNameLength;
	do 
	{
		if (lenCheck % 8 !=0)
		{
			lenCheck++;
		}

	} while (lenCheck % 8 !=0);
	if (uRec.recLen != lenCheck)
	{
		//sizes do not match
		return 0;
	}
	uRec.reasonText[0] = '\0';
	reasonString(&uRec);
	int written = writeUSN(&uRec);
	if (written == 1)
	{
		totalRecFound++;
		//updateStatus();
	}
	//clean up
	return 1;
}

int checkForRecords()
{
	if (fileBuffer[bufferPointer+1] == 0x00 & fileBuffer[bufferPointer+2] == 0x00 && fileBuffer[bufferPointer+3] == 0x00)
	{
		if (fileBuffer[bufferPointer + 4] == 0x02)
		{
			if (fileBuffer[bufferPointer + 58] == 60)
			{
				int result = extractRecord();
				if (result == 1)
				{
						bufferPointer += 60; 
				}
			}
		}
	}
	return 0;
}
Last edited on
from my knowledge, you don't have a "memory leak", as you don't use "new" anywhere in your code, so you don't dynamically allocate any blocks of memory. Thus no memory leak.

Assuming, this is correct, your "error" has to be coming from somewhere else...
In trying to fix my code I have tried to reduce my use of new in order to remove the bug, but no luck so far, my only guess is somewhere I concatenate a string with no null terminator or something.
I'm guessing you can't see where either :(
Topic archived. No new replies allowed.