Learning to use valgrind for memory Detection

Source: Internet
Author: User
Tags valgrind
There are three common memory leaks in C/C ++:

1. After malloc and realloc, they forget free; after new, they forget Delete; after new [], they forget Delete [].

2. Use the count method to manage the memory. The two count pointers direct to each other, leading to memory leakage.

3. Place the allocated and used memory in the container.

Among them, the memory leakage method in 3rd also exists in languages such as Java and C # That automatically release the memory.

The following is an example of Memory leakage caused by 2nd cases:

Valgrindmemcheck. cpp

#include <iostream>class A;class B;class A{public:    A()    :b(0),cnt(0)    {}    A(B *pB)    :b(pB),cnt(new int(1))    {}    A(const A &a)    :b(a.b),cnt(a.cnt)    {        ++*cnt;    }    A &operator =(const A &a);    ~A();private:    B *b;    int *cnt;};class B{public:    B()    :a(0),cnt(0)    {}    B(A *pA)    :a(pA),cnt(new int (1))    {}    B(const B &b)    :a(b.a),cnt(b.cnt)    {        ++*cnt;    }    B &operator =(const B &b);    ~B();private:    A *a;    int *cnt;};A &A::operator =(const A &a){    if(this == &a)        return *this;    if(cnt && 0 == --*cnt)    {        delete b;        delete cnt;    }    b = a.b;    cnt = a.cnt;    ++*cnt;    return *this;}A::~A(){    if(cnt && 0 == --*cnt)    {        delete b;        delete cnt;    }}B &B::operator =(const B &b){    if(this == &b)        return *this;    if(cnt && 0 == --*cnt)    {        delete a;        delete cnt;    }    a = b.a;    cnt = b.cnt;    ++*cnt;    return *this;}B::~B(){    if(cnt && 0 == --*cnt)    {        delete a;        delete cnt;    }}void test0(){    A a0;    B b0;    A a1(new B());    B b1(new A());}void test1(){    A *pA = new A();    B *pB = new B();    B b(pA);    A a(pB);}void test2(){    A a;    B b;    a = &b;    b = &a;}int main(void){    test0();    test1();    test2();    return 0;}

Makefile

all:ValgrindMemcheckValgrindMemcheck:ValgrindMemcheck.o        g++ -g -Wall -o $@ $^ValgrindMemcheck.o:ValgrindMemcheck.cpp        g++ -g -Wall -o $@ -c $<.PHONY:clean:        - $(RM) ValgrindMemcheck.cpp        - $(RM) ValgrindMemcheck.o

Which of the following memory leaks in test0, test1, and Test2?

Run:

Valgrind -- leak-check = full./valgrindmemcheck

We will find memory problems in test2.

References:

Http://www.ibm.com/developerworks/cn/linux/l-cn-valgrind/

Several blog posts are recommended:

Http://www.ibm.com/developerworks/cn/linux/l-cn-valgrind/

Http://blog.dccmx.com/2011/01/valgrind-massif/

Http://blog.dccmx.com/2011/01/callgrind/

Http://blog.dccmx.com/2011/01/gprof/

Http://blog.dccmx.com/2011/01/valgrind-memcheck/

Http://eduunix.ccut.edu.cn/

Http://www.oschina.net/project/tag/401/Neural-Network? Lang = 21 & sort = View

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.