3

在 C 中使用 MySQL 时,您可以使用 MySQL API 释放内存,如下所示:

MYSQL* connection = NULL;
connection = mysql_init(NULL);
// Stuff...
mysql_close(connection);

但是 Splint 不知道这mysql_close实际上是在释放内存,所以我收到了这个错误:

Fresh storage connection not released before return
A memory leak has been detected. Storage allocated locally is 
not released before the last reference to it is lost. (Use 
-mustfreefresh to inhibit warning)

我如何告诉 Splintmysql_close正在释放内存?文件的特殊注释mysql.h

编辑:好的,也许是releases *p注释,如果可以在头文件中使用的话。会尝试。

编辑 2:添加/*@releases *sock@*/mysql.h,但现在出现此错误:

Releases clauses includes *sock of non-dynamically allocated
              type MYSQL
A declaration uses an invalid annotation. (Use -annotationerror to inhibit
warning)

这是 的签名mysql_close

void STDCALL mysql_close(/*@notnull@*/ MYSQL *sock) /*@releases *sock@*/;
4

1 回答 1

1

我相信正确的注释是:

void STDCALL mysql_close(/*@special@*/ /*@notnull@*/ MYSQL *sock)
    /*@releases sock@*/;

您错过的关键是/*@special@*/注释,它是“激活”所谓的状态子句所必需的。从 Splint 的文档中,7.4 State Clauses

/*@special@*/注释用于标记使用状态子句描述的参数、全局变量或返回值。

于 2014-08-07T17:21:32.573 回答