---
title: "C/C++内存问题检查利器—Purify （三）"
author: "varsoft"
date: "2003-12-15T01:26:00Z"
tags: []
slug: "c-c-purify"
draft: false
lastmod: "2003-12-15T01:26:00Z"
id: 874244
url: "https://varsoft.iteye.com/blog/874244"
---

## 五、 文件描述符问题

在上面的内存问题表中，对于大多数的内存问题来说，相信对于熟悉C/C++的程序员，并不陌生。有一些关于Watchpoint和文件描述符的内容，可能会让你看得比较模糊，对于Watchpoint，我会在后面讲述。这一节，我就一个示例说一说文件描述述问题是如何产生的，并由此介绍一下Purify的一些特性。

先查看下面这段程序：

#include &lt;stdio.h&gt;

main()

{

FILE* fp;

int num;

fp = fopen("./test.txt", "r");

if ( fp == NULL){

perror("Error:");

exit(-1);

}

fscanf(fp, "%d", &num);

if ( num &lt; 0 ){

printf("Error: the num should be greater than 0!\\n");

exit(-1);

}

fclose(fp);

}

在当前目录下建一个test.txt的文件，并设置其内容为-20。使用Purify编译并运行程序：

&gt; purify gcc -g -o testfd testfd.c

Purify 2003.06.00 Solaris 2 (32-bit) Copyright (C) 1992-2002 Rational Software Corp. All rights reserved.

Instrumenting: ccqqF6pY.o Linking

&gt;./testfd

出现以下画面：

&lt;shapetype id="\_x0000\_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"&gt;&lt;stroke joinstyle="miter"&gt;&lt;/stroke&gt;&lt;formulas&gt;&lt;f eqn="if lineDrawn pixelLineWidth 0"&gt;&lt;/f&gt;&lt;f eqn="sum @0 1 0"&gt;&lt;/f&gt;&lt;f eqn="sum 0 0 @1"&gt;&lt;/f&gt;&lt;f eqn="prod @2 1 2"&gt;&lt;/f&gt;&lt;f eqn="prod @3 21600 pixelWidth"&gt;&lt;/f&gt;&lt;f eqn="prod @3 21600 pixelHeight"&gt;&lt;/f&gt;&lt;f eqn="sum @0 0 1"&gt;&lt;/f&gt;&lt;f eqn="prod @6 1 2"&gt;&lt;/f&gt;&lt;f eqn="prod @7 21600 pixelWidth"&gt;&lt;/f&gt;&lt;f eqn="sum @8 21600 0"&gt;&lt;/f&gt;&lt;f eqn="prod @7 21600 pixelHeight"&gt;&lt;/f&gt;&lt;f eqn="sum @10 21600 0"&gt;&lt;/f&gt;&lt;/formulas&gt;&lt;path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"&gt;&lt;/path&gt;&lt;lock aspectratio="t" v:ext="edit"&gt;&lt;/lock&gt;&lt;/shapetype&gt;

![](http://p.blog.csdn.net/images/p_blog_csdn_net/haoel/15190/o_purify6.jpg)

由图中，我们可以看到，Purify报告有FIU错误，意思是，我们在程序退出时，没有关闭文件描述符。还有一些算是安全的文件描述符信息，那就是关于0，1，2这三个标准文件描述符的FIU，这些信息是正常的，所以在其前面也就没有小三角符号了。

通过这个例子，我们可以看到，Purify不但可以找到内存的操作错误，还可以找到文件描述符的错误。

如果你不想让Purify显示FIU信息，你可以设置Purify的 -fds-inuse-at-exit=no 选项，如：

&gt; purify –fds-inuse-at-exit gcc -g -o testfd testfd.c

或者使用Purify的API函数 purify\_clear\_fds\_inuse 来阻止显示，你可以在你的程序中调用Purify的API函数。有关Purify的API函数的细节，我会在后面给你讲述。

## 六、 控制Purify的输出

**1、产生ASCII文本文件**

在默认情况下，Purify会显示出一个图形窗口来报告信息。当然，如果你的环境所限，你不想Purify出现图形界面，只是生成文本文件来报告，能过设置Purify的参数，你可以很容易做到这一点。

在程序编译时，你只需简单的调置Purify的编译参数 –windows=no 即可做到，如：

&gt; purify –windows=no gcc –g –o hello hello.c

Purify会把其报告信息写到标准错误设备上，在文本方式下，Purify就不报告同种错误出现在个数，而只是报告的信息了。

我们可以使用两种方式让Purify的信息输出到文本文件中。

第一种是使用操作系统的重定向功能，如：

在csh下： % a.out.pure &gt;& a.out.messages

在sh和ksh下： $ a.out.pure 2&gt; a.out.messages

第二种是指定Purify的日志文件参数，如：

-log-file=&lt;filename&gt;.plog

下面，是一个Purify生成的ASCII文本文件的样子：

&gt; ./hello

\*\*\** Purify instrumented hello (pid 25698 at Wed Dec 10 &lt;time minute="29" hour="22"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"&gt;22:29:33&lt;/span&gt;&lt;/time&gt; 2003)

\* Purify 2003.06.00 Solaris 2 (32-bit) Copyright (C) 1992-2002 Rational Software Corp. All rights reserved.

\* For contact information type: "purify -help"

\* Options settings: -follow-child-processes=yes -purify -windows=no \\

-purify-home=/usr/rational/releases/purify.sol.2003.06.00 \\

-gcc3\_path=/usr/local/bin/gcc \\

-cache-dir=/usr/rational/releases/purify.sol.2003.06.00/cache \\

-demangle\_program=/usr/local/bin/c++filt

\* License successfully checked out.

\* Command-line: ./hello

\*\*\** Purify instrumented hello (pid 25698) \*\*\**

ABR: Array bounds read:

\* This is occurring while in:

strlen \[rtlib.o]

\_doprnt \[libc.so.1]

printf \[libc.so.1]

main \[hello.c:11]

\_start \[crt1.o]

\* Reading 13 bytes from 0x8ea08 in the heap (1 byte at 0x8ea14 illegal).

\* Address 0x8ea08 is at the beginning of a malloc'd block of 12 bytes.

\* This block was allocated from:

malloc \[rtlib.o]

main \[hello.c:8]

\_start \[crt1.o]

Hello, World

\*\*\** Purify instrumented hello (pid 25698) \*\*\**

Current file descriptors in use: 5

FIU: file descriptor 0: &lt;stdin&gt;

FIU: file descriptor 1: &lt;stdout&gt;

FIU: file descriptor 2: &lt;stderr&gt;

FIU: file descriptor 26: &lt;reserved for Purify internal use&gt;

FIU: file descriptor 27: &lt;reserved for Purify internal use&gt;

\*\*\** Purify instrumented hello (pid 25698) \*\*\**

Purify: Searching for all memory leaks...

Memory leaked: 12 bytes (100%); potentially leaked: 0 bytes (0%)

MLK: 12 bytes leaked at 0x8ea08

\* This memory was allocated from:

malloc \[rtlib.o]

main \[hello.c:8]

\_start \[crt1.o]

Purify Heap Analysis (combining suppressed and unsuppressed blocks)

Blocks Bytes

Leaked 1 12

Potentially Leaked 0 0

In-Use 0 0

\----------------------------------------

Total Allocated 1 12

\*\*\** Purify instrumented hello (pid 25698) \*\*\**

\* Program exited with status code 13.

\* 1 access error, 1 total occurrence.

\* 12 bytes leaked.

\* 0 bytes potentially leaked.

\* Basic memory usage (including Purify overhead):

351348 code

101724 data/bss

8192 heap (peak use)

1272 stack

\* Shared library memory usage (including Purify overhead):

992 libpure\_solaris2\_init.so.1 (shared code)

280 libpure\_solaris2\_init.so.1 (private data)

1079516 libc.so.1\_pure\_p3\_c0\_111202132\_58\_32\_1158500S (shared code)

31404 libc.so.1\_pure\_p3\_c0\_111202132\_58\_32\_1158500S (private data)

2324 libdl.so.1\_pure\_p3\_c0\_111202132\_58\_32\_4624S (shared code)

4 libdl.so.1\_pure\_p3\_c0\_111202132\_58\_32\_4624S (private data)

14048 libinternal\_stubs.so.1 (shared code)

940 libinternal\_stubs.so.1 (private data)

**2、产生Purify自己的文件**

通过查看ASCII文本文件，我们发现其很不容易查看，特别是当错误很多时，而用在文件中没有源代码，看起来就是不如图形界面的好。但是我们为了把Purify的报告信息通过电子邮件传送给别人查看时，文件和图形界面兼得，我们可以使用Purify自己的文件，叫Purify View文件。我们可以使用Purify的图形界面打开这个文件，而来在图形化的窗口下查看。

我们可以有两种方式得到这个文件。一种是在Purify的图形界面的菜单中点击“File -&gt; Save as”来生成。第二种方法是使用Purify的 -view-file=&lt;filename&gt;.pv 参数来设置Purify View文件。

而要打开这个文件时，要么简单地在Purify的菜单中选取“Open”菜单，要么使用这样的命令：

% purify –view &lt;filename&gt;.pv

**3、自动发送邮件**

使用Purify的-mail-to-user参数可以方便地让Purify自动发送报告邮件。如：

% purify -mail-to-user=chris gcc ...

% purify -mail-to-user=chris,pat gcc ...

% purify -mail-to-user=devgrp gcc ...

在默认情况下，只要你设置了这个参数，Purify是不会打开图形界面窗口的，如果你要Purify打开图形窗口，那么你就一同使用 –windows=yes 参数。

**4、输出自己的信息**

如果你想在Purify中输出自己的信息，你可以在你的程序中使用Purify的API函数：

l purify\_printf(const char \*fmt, ...) 使用这个函数可以在Purify的图形界面，文件文件中输出你的自己的信息。

l purify\_logfile\_printf(const char \*fmt, ...) 使用这个函数可以在Purify的ASCII文本文件中输出你自己的信息。

l purify\_printf\_with\_call\_chain(const char \*fmt, ...) 使用这个函数可以在Purify的输出的同时，打印出函数调用栈的信息。这个函数和purify\_printf很类似。

注意，以上三个函数和标准C中的printf函数几乎是一样的，不过，这几个函数并不支持像printf函数中的所有%的格式，它仅支持：%d, %u, %n,%s, %c, %e, %f, 和 %g 这几种格式，并且就 %e %f %g 而且，并不支持其精度定义。

&lt;-[上一页](http://www.csdn.net/Develop/read_article.asp?id=22614) [下一页](http://www.csdn.net/Develop/read_article.asp?id=22696)-&gt;

**（版权所有，转载时请注明作者和出处）**