This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Memory leak in encode demo

Hello,
I'm using a DM365 evm and dvsdk_2_10_01_18 and noticed that the unmodified encode demo has a memory leak.
Chasing this, I found that in the file writer.c, commenting out the statement that writes the code to a file:

if (fwrite(Buffer_getUserPtr(hOutBuf),
                       Buffer_getNumBytesUsed(hOutBuf), 1, outFile) != 1) {
                ERR("Error writing the encoded data to video file\n");
                cleanup(THREAD_FAILURE);
            }

will stop the leak.  BUT, I'm not sure how to fix this as I still need to write out the encoded data.
Thanks,
-Vim

  • Hi Vim,

    This is strange. Neither Buffer_getUserPtr nor Buffer_getNumBytesUsed allocate any memory. Both are simple functions that return the requested value. And though possible, the chances of fwrite having a memory leak are small. How did you measure the leak? Did you do 'cat /proc/cmem' around the line? Also what type of memory is being leaked (memory managed by kernel or CMEM?)

    Best regards,

    Vincent

  • Hi Vincent, 

    I simply watched the free mem number in top as the application ran.  It monotonically decreases.  And yeah, it seems unlikely that fwrite would have a leak in it, but commenting out the write does stop the leak...

    So, any suggestions?

     

    Thanks,

    -Vim

  • Hi Vim,

    I think you may be hitting a known issue with file i/o. Before you exit your application, can you try adding the following lines:

        system("sync");
        system("echo 3 > /proc/sys/vm/drop_caches");

    This should help you "regain" your memory.

    Best regards,

    Vincent

  • Thanks Vincent, that does indeed get the memory back.

    -Vim

  • I too have discovered the same problem.

    The following:

    ./encode -v test.mpeg4

    Runs for a while but, the available memory decreases, until a kernel failure occurrs in my case. I suspect, like
    the original poster, that fwrite is to blame.

    Doing fflush() and fsync() should force the fwrite buffer to be written to file and then by doing the suggested  system calls, might free up the memory...
    However, it is not the most elegant solution I believe..


    So I ask, how is this "known issue with file i/o" solved?