c++ - Unable to spot Memory leak issue in below code -


i new c++. facing memory leak issue in c++ code. please see below mentioned piece of code, causing issue.

void a() {     char buffer[10000];     char error_msg[10000];     char log_file[filename_l] = "error_log.xml";      file *f;     f = fopen(log_file,"r");     while (fgets(buffer, 1000, f) != null)     {         if (strstr(buffer, " description: ") != null)         {             strcpy(error_msg, buffer);         }     }     fclose(f);       actual_error_msg = trimwhitespace(error_msg); } 

can please suggest on this. need use malloc instead of hardcoded size of array?

it seems there undefined behaviour if variable actual_error_msg global variable , function trimwhitespace not dynamically alocate memory copy of error_msg

actual_error_msg = trimwhitespace(error_msg); 

so when function finishes execution pointer actual_error_msg invalid.

can please suggest on this

i suggesting allocate dynamically memory copy of error_msg within function trimwhitespace. or if check whether memory freed in time.:)

take account looks strange buffer declared size equal 10000 while in fgets there used magic number 1000.

char buffer[10000]; //,,, while (fgets(buffer, 1000, f) != null) 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -