Memory leak

Dear Everyone,
Memory leak is happening and process is taking too much time to execute.
I m not understanding where i m going wrong can anyone help me out please
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

void writetransformfile(char *data,int curYear,int curMonth, int curDay)
{
	
	FILE *p;
	time_t    caltime;
	struct tm * broketime;
	char Day[10],Month[10],Year[10];
	char *str;
	struct tm *Sys_T = NULL;                     

	time_t Tval = 0;                            
	Tval = time(NULL);                          
	Sys_T = localtime(&Tval);                   
	sprintf(Day,"%d",Sys_T->tm_mday);
	sprintf(Month,"%d",Sys_T->tm_mon+1);
	sprintf(Year,"%d",Sys_T->tm_year + 1900);

	str = new char[2020];
  sprintf(str,"/usr/isac/aggregation/SA/data/%s%s%s.txt",Year,Month,Day);

	if(transform(data,curYear,curMonth,curDay))
	{
		p=fopen(str,"a");
		fwrite(data, strlen(data),1,p);
		fclose(p);
		//printf("trsanformed\n");
	}
	else
	{
		//printf("not transformed");
	}
	
	free(str);
}



int handlefile(char* filepath,int Year,int Month, int Day)
{
	FILE *finput;
	FILE *fHIST, *hist;
	char ch,hch;
	char *line;
	char *readLineNumber; 
	int hcnt, linecnt, totallinecnt;
	long cnt;	
	char *temp;
	char *histPath;
	int ignore;
	int sent;

 	gi = GeoIP_open("/u01/isac/install/GeoIP-1.4.8/data/GeoIPCity.dat", GEOIP_INDEX_CACHE);

	sent =0;
	cnt=0;
	hcnt=0;
	linecnt=0;
	totallinecnt = 0;
	histPath = new char[4096];
	temp = new char[4096];
	readLineNumber = new char[4096];
	readLineNumber[0] = '\0';
	line = new char[48000];
	line[0] = '\0';
	finput = fopen(filepath,"r");
	ignore = 0;
	if(finput != NULL)
	{
		sprintf(histPath,"/logs/isac/transformer/HIST/%s.HIST",basename(filepath));
		
		hist = fopen(histPath,"r");
		if(hist != NULL)
		{
			
			while(1)
			{
				hch = fgetc(hist);
				if(hch == EOF) break;
				readLineNumber[hcnt++] = hch;
				readLineNumber[hcnt] = '\0';
				
			}
		}
		while(1)
		{
			
			ch = fgetc(finput);
			if(feof(finput) || ch == EOF) 
			{
				printf("feof worked\n");
				break;
			}
			if(ch=='\n')
			{
				totallinecnt ++;
				if(totallinecnt > atoi(readLineNumber))
				{
				
					if(ignore == 0)
					{
						writetransformfile(line,Year,Month,Day);
						sent++;
					}

					sprintf(temp,"%d\0", totallinecnt);
					fHIST = fopen(histPath,"w");
					fwrite(temp, strlen(temp),1,fHIST);
					fclose(fHIST);
					printf("%d\n",totallinecnt);
					if(sent > 10000)
					{
						finput = NULL; 
						free(line);
						free(temp);
						free(readLineNumber);
						histPath = NULL;
						return 1;
					}
				}
				else
				{
					line[cnt++] = ch;
					line[cnt] = '\0';
				}
				cnt = 0;
				line[0] = '\0';	
			
			}
			else
			{
				line[cnt++] = ch;
				line[cnt] = '\0';
			}
		}	
	}
	finput = NULL; 
	free(line);
	free(temp);
	free(readLineNumber);
	//free(histpath);
	histPath = NULL;
return 0;
}

void error(const char *msg)
{
	perror(msg);
	exit(1);
}

int readfile(char *filename)
{
	time_t    caltime;
	struct tm * broketime;
	int checkreturn;
	char Day[10],Month[10],Year[10];
	char str[2048];
	struct tm *Sys_T = NULL;
	
	time_t Tval = 0;                            
	Tval = time(NULL);                          
	Sys_T = localtime(&Tval);                   
	sprintf(Day,"%d",Sys_T->tm_mday);
	sprintf(Month,"%d",Sys_T->tm_mon+1);
	sprintf(Year,"%d",Sys_T->tm_year + 1900);
	if(strstr(fromserver,"server1"))
	{
		sprintf(str,"/logs/isac/collation/data/logs1/%s",filename);
	} 
	else if(strstr(fromserver,"server2"))
	{
		sprintf(str,"/logs/isac/collation/data/logs2/%s",filename);
	}
  //  printf("full file path is %s\n",str);
	checkreturn=handlefile(str,atoi(Year),atoi(Month),atoi(Day));
	if(checkreturn==1)
	{
		return 1;
	}else
	{
		return 0; 
	}
	
}


int processlogfolder(char *logpath)
{ 
	char cmd[100];
	int checkreturn;
	struct dirent *sde=NULL;
	DIR *sd=NULL;
	char filename[2048];
	sd=opendir(logpath);
	if(sd == NULL)
	{
		perror("Couldn't open directory");
		return 0;
	}

	while(sde = readdir(sd))
	{
		if(strstr(sde->d_name,".log"))
		{
			sprintf(filename,sde->d_name);
			checkreturn=readfile(filename);
			if(checkreturn==1) 
			{
				closedir(sd);
				return 1;
			}
			if(strstr(fromserver,"server1"))
			{
					sprintf(cmd,"rm -f -r \"/logs/isac/collation/data/logs1/%s\"",filename);
					system(cmd);
			} 
			else if(strstr(fromserver,"server2"))
			{
				sprintf(cmd,"rm -f -r \"/logs/isac/collation/data/logs2/%s\"",filename);
				system(cmd);
			}
			 
			
		}
   
	}
	closedir(sd);
	return 0;
}




int main(int argc, char *argv[])
{
  char logfolderpath[2048];
  int checkreturn;
  sprintf(fromserver,"server1");
  sprintf(logfolderpath,"/logs/isac/collation/data/logs1/");
  checkreturn=processlogfolder(logfolderpath);

}
Last edited on
Do not use new with free.

If you use new, use delete. It you use malloc, use free. Do not mix them.
Dear Moschops.

Done but nothing change in preocess. There is something memory leak is happening and its taking too much time to process.
Can you please use the code format tag to format your code. It's near impossible to read as is.
My omnipresent request to edit your post so it uses code tags - the <> button on the right
So it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while(1)
{

ch = fgetc(finput);
if(feof(finput) || ch == EOF)
{
printf("feof worked\n");
break;
}
if(ch=='\n')
{
totallinecnt ++;
if(totallinecnt > atoi(readLineNumber))
{

if(ignore == 0)
{
writetransformfile(line,Year,Month,Day);
sent++;
}


Does the infinite loop cause problems? You don't break out of all the if conditions. There is another infinite loop outside this one as well.

There situations where infinite loops are a valid solution, but this doesn't look like one of them IMO.

Hope all goes well.

Edit: And some decent indenting would be nice too.
Last edited on
Topic archived. No new replies allowed.