COMP 2012 Object-Oriented Programming and Data Structures

Valgrind Tutorial

Tutorial

Preparation

Since valgrind is not available on Windows, and its macOS distribution is tricky to install in the latest macOS, we recommend you to run valgrind in the Linux Lab (Lab 2) by SSH. Here is a procedure to do this using Virtual Barn.

  1. Follow the procedures here to access Virtual Barn.
  2. Copy your files to Virtual Barn.
  3. Open PuTTY. There is a shortcut on your Desktop. Enter csl2wk13.cse.ust.hk as the Host Name.
  4. Click Open. If a warning about the security key shows up, click Yes to dismiss it.
  5. Enter your CSD login and password. You should login to your home directory. Enter the command pwd (Print Working Directory) to verify this. It should show /homes/your_login
  6. Open FileZilla from Start Menu. Click "File" -> "Site Manager" -> "New Site". Choose SFTP for protocol. Enter csl2wk13.cse.ust.hk as Host.
  7. Click Connect and then enter your CSD login and password.
  8. If the connection succeeds, the right side should show your home directory at Linux Lab. Copy your files by dragging them to the right side. You can close FileZilla after doing so.
  9. Go back to PuTTY, compile and run your program accordingly.

Running valgrind

Run your program with valgrind by:


                valgrind --leak-check=full ./pa2_test.exe
                # you can change the pa2_test.exe into other executable file path
              
A report should show up at the end of the output. If you see the following message, your program is free from memory leak:

                ==12712== HEAP SUMMARY:
                ==12712==     in use at exit: 0 bytes in 0 blocks
                ==12712==   total heap usage: 18 allocs, 18 frees, 720 bytes allocated
                ==12712==
                ==12712== All heap blocks were freed -- no leaks are possible
                ==12712==
                ==12712== For counts of detected and suppressed errors, rerun with: -v
                ==12712== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
              
But if you see the following messages instead, memory leaks could exist in your program:

                ==62364== LEAK SUMMARY:
                ==62364==    definitely lost: 8 bytes in 2 blocks
                ==62364==    indirectly lost: 0 bytes in 0 blocks
                ==62364==      possibly lost: 0 bytes in 0 blocks
                ==62364==    still reachable: 0 bytes in 0 blocks
                ==62364==         suppressed: 0 bytes in 0 blocks
              
OR

                ==13119== LEAK SUMMARY:
                ==13119==    definitely lost: 0 bytes in 0 blocks
                ==13119==    indirectly lost: 0 bytes in 0 blocks
                ==13119==      possibly lost: 0 bytes in 0 blocks
                ==13119==    still reachable: 560 bytes in 14 blocks
                ==13119==         suppressed: 0 bytes in 0 blocks
              

Menu
Page maintained by
  • Peng YUN
  • Email: pyun@cse.ust.hk
  • Last Modified:
Homepage