c - Memory leak on returning pointer -
i facing memory leak condition in following functions.
char * readdatafromfile(unsigned pagenumber) { char *buff = (char*) malloc(sizeof(char) * pagesize); lseek(fd, pagesize * (pagenumber), seek_set); read(fd, buff, pagesize); return buff; } //read file char * readfromfile(char *fname, int pageno) { char *buff = null; fd = searchinvector(fname); if (fd > 0) buff = readdatafromfile(pageno); else printf("\nindex not opened\n"); return buff; }
i trying call function following way
char* root_buf = readfromfile(fname,pageno);
can point me memory leak occurs , how overcome it.
edit
i call free(root_buf); later. forgot mention part. believe has fact creating pointer , returning it. maybe reference caught in pointer in caller function.
the memory allocated using malloc
never freed again. if call site:
char* root_buf = readfromfile(fname,pageno); // stuff free(root_buf);
it should solve leak.
Comments
Post a Comment