Changes between Version 5 and Version 6 of cypress/Programming/CodeDebugging


Ignore:
Timestamp:
08/20/15 21:45:29 (9 years ago)
Author:
fuji
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • cypress/Programming/CodeDebugging

    v5 v6  
    317317}}}
    318318
     319
     320=== Detect Memory Leaks  ===
     321
     322Example code: (this code has a bug)
     323
     324{{{#!c++
     325#include <iostream>
     326#include <cstring>
     327
     328char * foo() {
     329  char *a = new char[200];
     330  std::strcpy(a, "hello workshop");
     331  return a;
     332}
     333
     334int main() {
     335  char * a = foo();
     336  char * b = foo();
     337  std::cout << "a = " <<  a << std::endl;
     338  std::cout << "b = " <<  b << std::endl;
     339  return 0;
     340}
     341}}}
     342
     343{{{#!bash
     344[fuji@cypress1 TestCodes]$ icpc -g mleak.cpp
     345[fuji@cypress1 TestCodes]$ valgrind --leak-check=full ./a.out
     346==10272== Memcheck, a memory error detector
     347==10272== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
     348==10272== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
     349==10272== Command: ./a.out
     350==10272==
     351a = hello workshop
     352b = hello workshop
     353==10272==
     354==10272== HEAP SUMMARY:
     355==10272==     in use at exit: 400 bytes in 2 blocks
     356==10272==   total heap usage: 2 allocs, 0 frees, 400 bytes allocated
     357==10272==
     358==10272== 200 bytes in 1 blocks are definitely lost in loss record 1 of 2
     359==10272==    at 0x4C28192: operator new[](unsigned long) (vg_replace_malloc.c:363)
     360==10272==    by 0x4009D8: foo() (mleak.cpp:5)
     361==10272==    by 0x400A0F: main (mleak.cpp:11)
     362==10272==
     363==10272== 200 bytes in 1 blocks are definitely lost in loss record 2 of 2
     364==10272==    at 0x4C28192: operator new[](unsigned long) (vg_replace_malloc.c:363)
     365==10272==    by 0x4009D8: foo() (mleak.cpp:5)
     366==10272==    by 0x400A20: main (mleak.cpp:12)
     367==10272==
     368==10272== LEAK SUMMARY:
     369==10272==    definitely lost: 400 bytes in 2 blocks
     370==10272==    indirectly lost: 0 bytes in 0 blocks
     371==10272==      possibly lost: 0 bytes in 0 blocks
     372==10272==    still reachable: 0 bytes in 0 blocks
     373==10272==         suppressed: 0 bytes in 0 blocks
     374==10272==
     375==10272== For counts of detected and suppressed errors, rerun with: -v
     376==10272== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)
     377}}}
     378
    319379== Intel® Inspector XE ==
    320380Memory and Thread Debugger: