dmalloc用法快速入門

2021-09-08 01:21:03 字數 3261 閱讀 7810

常用記憶體洩露檢測手段有

dmalloc是乙個簡單易用的c/c++記憶體leak檢查工具,以乙個執行庫的方式發布。

dmalloc能夠檢查出直到程式執行結束還沒有釋放的記憶體,並且能夠精確指出在

哪個原始檔的第幾行。

dmalloc 主頁: 

支援的平台:aix, bsd/os, dg/ux, free/net/openbsd, gnu/hurd, hpux, irix, linux, ms-dog, next, osf, sco, solaris, sunos, ultrix, unixware, windoze, and even unicos on a cray t3e

最新版本: 5.5.2

tar zxvf dmalloc-5.5.2.tgz

cd dmalloc-5.5.2

./configure

make

make install

設定環境變數:

對於 bash, ksh, and zsh ( 在 `.bashrc', `.profile', or `.zshrc'

檔案中加入一行 ( -b 引數表示針對bash的輸出):

function dmalloc

然後重新登陸使用者,或者執行: source ~/.bashrc 或 source ~/.profile

接下面執行:

dmalloc -l logfile -i 100 low

在原始檔中新增下面的c**:

#ifdef dmalloc

#include "dmalloc.h"

#endif

編譯使用的cflags: -ddmalloc -ddmalloc_func_check

如: gcc -ddmalloc -ddmalloc_func_check dm_test.c

執行:./a.out

執行上面的命令會在當前目錄下生成 logfile檔案,檢視logfile的內容如下:

cat logfile

1214894489: 2: dmalloc version '5.5.2' from '/'

1214894489: 2: flags = 0x4e48503, logfile 'logfile'

1214894489: 2: interval = 100, addr = 0, seen # = 0, limit = 0

1214894489: 2: starting time = 1214894489

1214894489: 2: process pid = 9560

1214894489: 2: dumping chunk statistics:

1214894489: 2: basic-block 4096 bytes, alignment 8 bytes

1214894489: 2: heap address range: 0xb8020000 to 0xb8029000, 36864 bytes

1214894489: 2:     user blocks: 1 blocks, 4043 bytes (10%)

1214894489: 2:    admin blocks: 8 blocks, 32768 bytes (89%)

1214894489: 2:    total blocks: 9 blocks, 36864 bytes

1214894489: 2: heap checked 1

1214894489: 2: alloc calls: malloc 2, calloc 0, realloc 0, free 0

1214894489: 2: alloc calls: recalloc 0, memalign 0, valloc 0

1214894489: 2: alloc calls: new 0, delete 0

1214894489: 2:   current memory in use: 11 bytes (2 pnts)

1214894489: 2:  total memory allocated: 11 bytes (2 pnts)

1214894489: 2:  max in use at one time: 11 bytes (2 pnts)

1214894489: 2: max alloced with 1 call: 6 bytes

1214894489: 2: max unused memory space: 53 bytes (82%)

1214894489: 2: top 10 allocations:

1214894489: 2:  total-size  count in-use-size  count  source

1214894489: 2:           6      1           6      1  dm_test.c:71

1214894489: 2:           5      1           5      1  dm_test.c:69

1214894489: 2:          11      2          11      2  total of 2

1214894489: 2: dumping not-freed pointers changed since start:

1214894489: 2:  not freed: '0xb8028fc8|s1' (6 bytes) from 'dm_test.c:71'

1214894489: 2:  not freed: '0xb8028fe8|s1' (5 bytes) from 'dm_test.c:69'

1214894489: 2:  total-size  count  source

1214894489: 2:           6      1  dm_test.c:71

1214894489: 2:           5      1  dm_test.c:69

1214894489: 2:          11      2  total of 2

1214894489: 2: ending time = 1214894489, elapsed since start = 0:00:00

那麼,哪個地方的記憶體leak就一目了然了。

//*****= dm_test.c 源** **********===

#include

#include

#include

#ifdef dmalloc

#include

#endif

int main(int argc, char **ar**)

C 快速入門 exern關鍵字的用法

extern是c c 語言中表明函式和全域性變數作用範圍 可見性 的關鍵字.它告訴編譯器,其宣告的函式和變數可以在本模組或其它模組中使用。1。對於extern變數來說,僅僅是乙個變數的宣告,其並不是在定義分配記憶體空間。如果該變數定義多次,會有連線錯誤 2。通常,在模組的標頭檔案中對本模組提供給其它...

Flask入門二 快速入門

from flask import flask 路由裝飾器 defhello world 檢視函式 return hello world 返回乙個字串 if name main 把程式儲存為hello.py 用python直譯器執行 python hello.py running on訪問 會看見 ...

adb shell 入門 ADB快速入門

一 簡介 android debug bridge,我們一般簡稱為adb,主要存放在sdk安裝目錄下的platform tools資料夾中,它是乙個非常強大的命令列工具,通過這個工具用來連線電腦和android裝置 手機 電腦 電視 平板 電視盒子等 二 安裝 提取碼 54zh 2.解壓檔案包到固定...