Implement a simple memory leak detection Tool VLD

Source: Internet
Author: User

There is a well-known memory leak detection Tool visual leak detected presumably everyone is not unfamiliar, but today we can write a simple version of our own. Haha, do your own hands, clothed there are wood!!!

It's the principle that we overloaded the operator new and delete, and when we open up space with new, we say that this space is strung into a list of memnode formed by our defined structure, (this is the teacher's writing, at the end of the main program, with CHECK_VLD () function detects there is no memory leak) but I think that if we dynamically open up an object, it is not feasible to use the delete release in its destructor. Because the destructor is called only when it is return (before the CHECK_VLD () function). The test results are inaccurate. From this I think, you can put this struct as class Memnode in C + +, and encapsulate its pointer as a class Pmemnode, and then define a Pmemnode global static member (it will be constructed before the main function, and the main function ends after the destructor). and put the code for CHECK_VLD () in his destructor.

Nonsense not much to say, the code is below:

Vld.h:

#pragma once#include <iostream>using namespace Std;class memnode {friend void* operator new (size_t sz, char *filenam e, int line);p ublic:memnode (int s = 0,char *PF = NULL, int l = 0,class memnode *pl=null): Size (s), file (PF), line (l), link (PL) {}~memnode () {}void set (int sz, char *pf, int L, class Memnode *pl) {size = Sz;file = Pf;line = L;link = pl;} void Setlink (Memnode *pl) {link = pl;} int GetSize () {return size;} Char *getfile () {return file;} int Getline () {return line;} Memnode *getlink () {return link;} Private:int Size;char *file;int line;class memnode *link;}; void Check_vld (); class Pmemnode {Public:pmemnode (Memnode *ptr=null):p (PTR) {}~pmemnode () {CHECK_VLD ();} Memnode *getptr () {return p;} void Setptr (Memnode *ptr) {p = ptr;} Private:memnode *p;}; Static Pmemnode _afxmem;void* operator new (size_t Sz,char *filename,int line) {void *result=null;int total_size = sz + size of (Memnode); Memnode *p = new Memnode[total_size];p->set (SZ, filename, line, NULL); if (_afxmem.getptr () = = NULL) {_afxmem.setptr (P);} Else{p->setlink (_afxmem.getptr ()); _afxmem.setptr (P);} result = _afxmem.getptr () +1;return result;} void operator delete (void *ptr) {if (_afxmem.getptr () ==null) {return;} if (ptr==null) {return;} Memnode *p;if (_afxmem.getptr () +1 = = ptr) {p = _afxmem.getptr (); _afxmem.setptr (P->getlink ());d elete p;} Else{p = _afxmem.getptr (); while (P->getlink ()! = NULL && p->getlink () +1!=ptr) {p = P->getlink ();} if (p->getlink () = NULL) {Memnode *q = P->getlink ();p->setlink (Q->getlink ());d elete Q;}}} void Check_vld () {if (_afxmem.getptr () = = NULL) {cout << "No memory Leaks detected." << Endl;} Else{memnode *p = _afxmem.getptr () cout << "Warning:visual Leak Detector detected memory leaks!" << Endl;whil E (P! = NULL) {printf ("at%p:%d bytes\n", p + 1, p->getsize ());p rintf ("file:%s, line:%d\n", P->getfile (), P->ge Tline ());p = P->getlink ();}}}

Main.cpp:

#include <iostream> #include <string> #include "vld.h" using namespace Std;class test{public:test (char *str) {int a = strlen (str) + 1;ch = new (__file__, __line__) char[a];//strcpy (ch, str);} ~test () {delete[]ch;ch = NULL;} Private:char *ch;int a;double B;char c;}; int main () {//vld vld;int a = 10; Test t ("Hello");//int *p = new (__file__,__line__) int (ten);//double *q = new (__file__,__line__) double (12.34); Test *s = new (__file__,__line__) test ("Hello"),//delete p;//delete q;//check_vld (); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implement a simple memory leak detection Tool VLD

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.