I am building an array and I want it to be a fixed size so that as I read in a file it only stores the last 10 commands. The file seems to read in correctly and it looks right when I print it but for some reason my memory is not getting freed. MAX is set to 1000 and historySize is read earlier from the user. I ran valgrind on my code and when the calls to these functions are commented out I do not have any leaks.
I have a char ** history under my #includes
Here is my code
void setupHistoryFile()
{
char string[MAX];
FILE *fp;
int len;
int pos = 0;
fp = fopen(".ush_history","r");
if(fp == NULL)
{
//create the file
fp = fopen(".ush_history","w");
}
else
{
history = (char**)malloc(historySize * sizeof(char*));//setup history file
fgets(string,MAX,fp);
len = strlen(string);
if(string[len-1]=='\n')
string[len-1]='\0';
while(!feof(fp))
{
if(history[pos] != NULL)
{
free(history[pos]);
history[pos]=NULL;
}
history[pos] = (char*)malloc((strlen(string)+1) * sizeof(char));
//printf("Should be copying %s\n",string);
strcpy(history[pos], string);
pos++;
pos = pos % historySize;
fgets(string,MAX,fp);
len = strlen(string);
if(string[len-1]=='\n')
string[len-1]='\0';
}
}
fclose(fp);
}
I do have a function that cleans the history and it looks like this
void cleanHistory()
{
int i;
if(history != NULL)
{
for(i=0;i<historySize;i++)
{
free(history[i]);
history[i] = NULL;
}
free(history);
history = NULL;
}
}
cleanHistory()is getting called on the very same pointer array to which you allocated the memory? You can put breakpoints incleanHistory()and check if this functions gets called at all.