轉自:http://hi.baidu.com/askc037/blog/item/3b20e0cd1d2cea570eb345e1.html
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void foo()
{
char *p = NULL;
p = (char *)malloc(1);
}
class LeakClass
{
public:
LeakClass()
{
cout<<"No Parameter Constructor"<<endl;
foo();
}
LeakClass(const char *name)
{
cout<<"Parameter is:"<<name<<endl;
}
};
int main(int argc, char **argv)
{
LeakClass *P = NULL;
cout << "Begin..." <<endl;
sleep(10);
while (1)
{
//foo();
P = new LeakClass();
P = new LeakClass("Hello");
delete(P);
sleep(10);
//break;
}
//cout << "End...Sleep" <<endl;
//sleep(50);
return 0;
}
-----------------------------------
root@MassonSolaris:~/Desktop/MyDtrace# dbx ./a.out
For information about new features see `help changes'
To remove this message, put `dbxenv suppress_startup_message 7.6' in your .dbxrc
Reading a.out
Reading ld.so.1
Reading libumem.so.1
Reading libCstd.so.1
Reading libCrun.so.1
Reading libm.so.2
Reading libc.so.1
(dbx) check -leaks
leaks checking - ON
(dbx) dbxenv rtc_mel_at_exit verbose
(dbx) run
Running: a.out
(process id 1686)
Reading rtcapihook.so
Reading libdl.so.1
Reading rtcaudit.so
Reading libmapmalloc.so.1
Reading libgen.so.1
Reading rtcboot.so
Reading librtc.so
RTC: Enabling Error Checking...
RTC: Running program...
Begin...
No Parameter Constructor
Parameter is:Hello
Checking for memory leaks...
Actual leaks report (actual leaks: 2 total size: 2 bytes)
Memory Leak (mel):
Found leaked block of size 1 byte at address 0x8067c50
At time of allocation, the call stack was:
[1] operator new() at 0xbd90662b
[2] main() at line 34 in "MemoryLeak07.cpp"
Memory Leak (mel):
Found leaked block of size 1 byte at address 0x8067c70
At time of allocation, the call stack was:
[1] foo() at line 9 in "MemoryLeak07.cpp"
[2] LeakClass::LeakClass() at line 18 in "MemoryLeak07.cpp"
[3] main() at line 34 in "MemoryLeak07.cpp"
Possible leaks report (possible leaks: 0 total size: 0 bytes)
execution completed, exit code is 0
(dbx)
---------------------------------
root@MassonSolaris:~/Desktop/MyDtrace# export LD_AUDIt=/opt/SUNWspro/lib/rtcaudit.so;
root@MassonSolaris:~/Desktop/MyDtrace# export LD_AUDIT=/opt/SUNWspro/lib/rtcaudit.so;
root@MassonSolaris:~/Desktop/MyDtrace# echo $LD_AUDI
root@MassonSolaris:~/Desktop/MyDtrace# ps -a
PID TTY TIME CMD
1738 pts/2 0:00 a.out
588 ? 0:00 <defunct>
688 pts/2 1:23 gedit
1739 pts/3 0:00 ps
root@MassonSolaris:~/Desktop/MyDtrace# dbx ./a.out 1738
For information about new features see `help changes'
To remove this message, put `dbxenv suppress_startup_message 7.6' in your .dbxrc
Reading a.out
Reading ld.so.1
Reading libumem.so.1
Reading libCstd.so.1
Reading libCrun.so.1
Reading libm.so.2
Reading libc.so.1
Reading rtcaudit.so
Reading libmapmalloc.so.1
Reading libgen.so.1
Reading libdl.so.1
Reading rtcboot.so
Reading librtc.so
Attached to process 1738
stopped in __nanosleep at 0xd07846f5
0xd07846f5: __nanosleep+0x0015: jae __nanosleep+0x23 [ 0xd0784703, .+0xe ]
Current function is main
37 sleep(10);
dbx: internal warning: set_error_limit called too early
(dbx) dbxenv mt_sync_tracking off
(dbx) check -leaks
leaks checking - ON
RTC: Enabling Error Checking...
RTC: Running program...
(dbx) showleaks -v -a
Checking for memory leaks...
Actual leaks report (actual leaks: 2 total size: 2 bytes)
Memory Leak (mel):
Found leaked block of size 1 byte at address 0x8067c88
At time of allocation, the call stack was:
[1] operator new() at 0xd089662b
[2] main() at line 34 in "MemoryLeak07.cpp"
Memory Leak (mel):
Found leaked block of size 1 byte at address 0x8067ca8
At time of allocation, the call stack was:
[1] foo() at line 9 in "MemoryLeak07.cpp"
[2] LeakClass::LeakClass() at line 18 in "MemoryLeak07.cpp"
[3] main() at line 34 in "MemoryLeak07.cpp"
Possible leaks report (possible leaks: 0 total size: 0 bytes)
(dbx)