memory leak on read directory example?
Posted: Sat Sep 20, 2008 9:08 am
I'm chasing a memory leak in my homebrew project...
I'm using this read directory code from the ogg player example...
the memory allocated for
current_dir, filename_array & fileattrib_array
is not freed: is it ok or is it a misconception of the code?
Thanks
I'm using this read directory code from the ogg player example...
Code: Select all
filenames_t* readDirectory (char* directory) {
int ret=-1;
int n_dir=0;
int n_reg=0;
fio_dirent_t record;
char** filename_array =NULL;
int i =0;
filenames_t* current_dir =NULL;
int* fileattrib_array =NULL;
/*Create space */
current_dir = malloc(sizeof(*current_dir));
filename_array = malloc(sizeof(char*)*FILEARRAYBUFFER);
fileattrib_array = malloc(sizeof(int)*FILEARRAYBUFFER);
/*Open Directory */
if ((ret = fioDopen(directory)) < 0) {
//printf("Error opening dir\n");
return NULL;
}
i=0;
n_dir=0;
n_reg=0;
while (fioDread(ret, &record) > 0) {
/*Expand array if too small */
if (i % FILEARRAYBUFFER == 0) {
filename_array = realloc(filename_array,
sizeof(char*) * (i+FILEARRAYBUFFER));
fileattrib_array = realloc(fileattrib_array,
sizeof(int) * (i+FILEARRAYBUFFER));
}
/*Copy filename into array */
filename_array[i] = malloc(strlen(record.name)+1);
strcpy(filename_array[i], record.name);
fileattrib_array[i] = record.stat.mode;
/*Keep track of number of files */
if (FIO_SO_ISDIR(record.stat.mode)) {
n_dir++;
}
if (FIO_SO_ISREG(record.stat.mode)) {
n_reg++;
}
i++;
}
if (ret >= 0) fioDclose(ret);
current_dir->filenames = filename_array;
current_dir->num_filenames = i;
current_dir->attrib = fileattrib_array;
return current_dir;
}
current_dir, filename_array & fileattrib_array
is not freed: is it ok or is it a misconception of the code?
Thanks