1 / 11

CSCE-221 Valgrind

CSCE-221 Valgrind. Emil Thomas 02/28/19. What is Valgrind. Is a tool suite that contains various debugging and profiling tools Most popular tool : Memcheck – Can detect many Heap memory related errors Used with C/C++ programs for finding Memory Mismanagement Available in Linux.

alfredok
Download Presentation

CSCE-221 Valgrind

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCE-221Valgrind Emil Thomas 02/28/19

  2. What is Valgrind • Is a tool suite that contains various debugging and profiling tools • Most popular tool : Memcheck – Can detect many Heap memory related errors • Used with C/C++ programs for finding Memory Mismanagement • Available in Linux

  3. Features of Valgrind • Can detect the use of uninitialized memory in Heap • Reading/writing memory after it has been deallocated using delete • Memory leaks -- where pointers to new'd blocks are lost forever • Mismatched use of new/new [] vs delete/delete []

  4. Limitations of Valgrind • Occasionally can have false positives • Cannot detect out-of-range read/write on Stack allocated arrays

  5. Why should we Check Memory leak? • Performance Tuning • Prevent run time Errors • Can help fixing segmentation fault errors • Saves hours of debugging • Can increase the security of application

  6. Invoking valgrind (Command line) • The C++ program must be compiled with debugging info (-g flag) • g++ -std=c++11 –g Driver.cpp –o driver.out • valgrind ./driver.out

  7. Avoiding Memory Leak • Every new allocated objects (Objects in Heap) should have a matching delete • Person *Frank = new Person(); ……. delete Frank; • int *ptr = new int [50]; ……. delete [] ptr; • Should not need to call delete on stack allocated objects int main() { Person Foo; }

  8. Example output from Valgrind

  9. Live Demo

  10. Exercise to be uploaded to ecampus && Do Survey !!! • Login to linux2.cs.tamu.edu or compute.cs.tamu.edu using your netid and password • Go to your csce221 folder • cd csce221 • Copy the cpp file • wget http://faculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/Valgrind/fixMem.cpp • Compile and run the code • g++ -std=c++11 –g fixMem.cpp –o driver.out • ./driver.out • Run Valgrind • valgrind ./driver.out • Change the code to fix any memory related errors (i.evalgrind output should be clean) • Upload the fixed cpp file in ecampus under lab7_valgrind

  11. References: • http://valgrind.org/ • http://valgrind.org/docs/manual/valgrind_manual.pdf real_time • Notes from Prof. Lupoli • http://cs.ecs.baylor.edu/~donahoo/tools/valgrind/

More Related