30、valgrind之一二维指针形式上的内存释放

基本思想:继续回顾valgrind的内存释放的理论和方法;

#include<iostream>
#include<malloc.h>
using namespace std;
char* getSpace()
{
	char *p = (char *)malloc(30); 
	return p;
}

int main()
{
	char *p = getSpace();
	if(NULL!=p)
	{ free(p);
		p=NULL;
	}
	return 0;
}

valgrind释放内存:

ubuntu@ubuntu:~/Downloads$ G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --tool=memcheck --leak-check=full --leak-resolution=high --num-callers=20 --show-reachable=yes --log-file=a.log  ./a.out
ubuntu@ubuntu:~/Downloads$ cat a.log 
==13038== Memcheck, a memory error detector
==13038== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==13038== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==13038== Command: ./a.out
==13038== Parent PID: 11113
==13038== 
==13038== 
==13038== HEAP SUMMARY:
==13038==     in use at exit: 72,704 bytes in 1 blocks
==13038==   total heap usage: 2 allocs, 1 frees, 72,734 bytes allocated
==13038== 
==13038== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==13038==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13038==    by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==13038==    by 0x40106F9: call_init.part.0 (dl-init.c:72)
==13038==    by 0x401080A: call_init (dl-init.c:30)
==13038==    by 0x401080A: _dl_init (dl-init.c:120)
==13038==    by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==13038== 
==13038== LEAK SUMMARY:
==13038==    definitely lost: 0 bytes in 0 blocks
==13038==    indirectly lost: 0 bytes in 0 blocks
==13038==      possibly lost: 0 bytes in 0 blocks
==13038==    still reachable: 72,704 bytes in 1 blocks
==13038==         suppressed: 0 bytes in 0 blocks
==13038== 
==13038== For counts of detected and suppressed errors, rerun with: -v
==13038== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

 例子

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
using namespace std;
void getSpace(char *p)
{
	p = (char *)malloc(30); 
	memset(p,'1',sizeof(char)*30);	
        cout<<p<<endl;
}//形式参数int *p在栈空间内,函数结束后就释放了,malloc分配的空间丢失

int main()
{
	char *p =NULL;
	getSpace(p);

	cout<<p<<endl;
	return 0;
}

valgrind检查 存在内存泄露 堆空间丢失

ubuntu@ubuntu:~/Downloads$ cat a.log 
==22024== Memcheck, a memory error detector
==22024== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==22024== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==22024== Command: ./a.out
==22024== Parent PID: 11113
==22024== 
==22024== Invalid read of size 1
==22024==    at 0x4C30F74: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22024==    by 0x4F49228: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==22024==    by 0x400916: getSpace(char*) (in /home/ubuntu/Downloads/a.out)
==22024==    by 0x400942: main (in /home/ubuntu/Downloads/a.out)
==22024==  Address 0x5ab6c9e is 0 bytes after a block of size 30 alloc'd
==22024==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22024==    by 0x4008EB: getSpace(char*) (in /home/ubuntu/Downloads/a.out)
==22024==    by 0x400942: main (in /home/ubuntu/Downloads/a.out)
==22024== 
==22024== 
==22024== HEAP SUMMARY:
==22024==     in use at exit: 72,734 bytes in 2 blocks
==22024==   total heap usage: 3 allocs, 1 frees, 73,758 bytes allocated
==22024== 
==22024== 30 bytes in 1 blocks are definitely lost in loss record 1 of 2
==22024==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22024==    by 0x4008EB: getSpace(char*) (in /home/ubuntu/Downloads/a.out)
==22024==    by 0x400942: main (in /home/ubuntu/Downloads/a.out)
==22024== 
==22024== 72,704 bytes in 1 blocks are still reachable in loss record 2 of 2
==22024==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22024==    by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==22024==    by 0x40106F9: call_init.part.0 (dl-init.c:72)
==22024==    by 0x401080A: call_init (dl-init.c:30)
==22024==    by 0x401080A: _dl_init (dl-init.c:120)
==22024==    by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==22024== 
==22024== LEAK SUMMARY:
==22024==    definitely lost: 30 bytes in 1 blocks
==22024==    indirectly lost: 0 bytes in 0 blocks
==22024==      possibly lost: 0 bytes in 0 blocks
==22024==    still reachable: 72,704 bytes in 1 blocks
==22024==         suppressed: 0 bytes in 0 blocks
==22024== 
==22024== For counts of detected and suppressed errors, rerun with: -v
==22024== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

类的二维指针形式;

#include<iostream>
#include<string.h>
using namespace std;

class AAA{
	public:
		AAA();
		~AAA();
	public:
		char *label;

};

class AA
{
	public:
		AA(int num);
		~AA();
		void add(int index,char* b);
	private:
		int n;
		AAA **aaa;

};
AAA::AAA()
{
label=NULL;
}

AAA::~AAA()
{
if(NULL!=label)
{
delete[] label;
}
}

AA::AA(int num)
{
	aaa=new AAA*[num];
	for(int i=0;i<num;i++)
	{aaa[i]=NULL;
	}
	n=num;
}
AA::~AA(){
	for(int i=0;i<n;i++)
	{
		if(NULL!=aaa[i])
		{
			delete aaa[i];
			aaa[i]=NULL;
		}
		
	}
delete [] aaa;
}
void AA::add(int index,char* b)
{
	aaa[index]=new AAA();
	aaa[index]->label=new char[strlen(b)+1];
	memcpy(aaa[index]->label,b,strlen(b)+1);
}

int main()
{
        char *s="hello world";
	AA *aa=new AA(2);
	aa->add(0,s);
	aa->add(1,s);
	delete aa;
	return 0;
}

valgrind

ubuntu@ubuntu:~$ G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --tool=memcheck --leak-check=full --leak-resolution=high --num-callers=20 --show-reachable=yes --log-file=a.log  ./a.out
ubuntu@ubuntu:~$ cat a.log 
==485== Memcheck, a memory error detector
==485== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==485== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==485== Command: ./a.out
==485== Parent PID: 28526
==485== 
==485== 
==485== HEAP SUMMARY:
==485==     in use at exit: 72,704 bytes in 1 blocks
==485==   total heap usage: 7 allocs, 6 frees, 72,776 bytes allocated
==485== 
==485== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==485==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==485==    by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==485==    by 0x40106F9: call_init.part.0 (dl-init.c:72)
==485==    by 0x401080A: call_init (dl-init.c:30)
==485==    by 0x401080A: _dl_init (dl-init.c:120)
==485==    by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==485== 
==485== LEAK SUMMARY:
==485==    definitely lost: 0 bytes in 0 blocks
==485==    indirectly lost: 0 bytes in 0 blocks
==485==      possibly lost: 0 bytes in 0 blocks
==485==    still reachable: 72,704 bytes in 1 blocks
==485==         suppressed: 0 bytes in 0 blocks
==485== 
==485== For counts of detected and suppressed errors, rerun with: -v
==485== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
ubuntu@ubuntu:~$ 

猜你喜欢

转载自blog.csdn.net/sxj731533730/article/details/107590018