Memory Leaking - C -
in same file have 2 routines. first store bytes 1 file. other give information routines process information.
boolean adin_memory(char* buffer, int size_chunck, int end_flag){ real_data=(sp16 *)malloc(size_chunck); //real_data -->global memcpy(&(real_data[0]),&(buffer[0]),size_chunck); pos_write += size_chunck; global_size = size_chunck; global_end_flag = end_flag; //end_flag = 1 --> end of stream //end_flag = 0 --> streaming return true; }
to prevent possibility of leaking using malloc
. routine called several times. so, after repetitions of adin_memory
, adin_read
(where free
), think memory starts fragment (i can see leak size of input file in task manager - increment of ram). right? how can prevent this? see leak put 1 breakpoint @ beginning , @ end of adin_memory @ task manager.
int adin_read(sp16 *buf, int sampnum) { file *fp; int cnt = 0; fp = gfp; //(.......) if(global_end_flag == 1 || pos_write == pos_read){ return -1;} for(i = pos_read/sizeof(sp16); <= sampnum; i++){ if(i >= pos_write/sizeof(sp16)) { cnt = i; //(....) break; } buf[i] = real_data[i]; } pos_write = 0; //(....) free(real_data); return cnt; }
int adin_read(sp16 *buf, int sampnum) { file *fp; int cnt = 0; fp = gfp; //(.......) if(global_end_flag == 1 || pos_write == pos_read){ /* leak possibly here. return without freeing. ensure free called here also. , practice make freed pointer point null can check , avoid double free problems. */ return -1; } for(i = pos_read/sizeof(sp16); <= sampnum; i++){ if(i >= pos_write/sizeof(sp16)) { cnt = i; //(....) break; } buf[i] = real_data[i]; } pos_write = 0; //(....) free(real_data); return cnt; }
Comments
Post a Comment