私はスプリントの使い方を学びながら、C を再学習しようとしています。 安全第一!
その中にファイルポインターを持つ構造があります。ファイル ポインタは、コンストラクタ関数内で開かれ、デストラクタ関数内で閉じられます。構造体型定義で注釈が付けられて/@only@/
おり、splint は、構造体内のファイル ポインターがそのメモリへの唯一のポインター (以下の詳細を参照) であることを認識しているようです。
デストラクタ関数では、ファイル ポインタが null でない限り、ファイルは閉じられます。
ただし、 splint は、 filepointer である限りファイルが閉じられているにもかかわらず、ファイル ポインターが解放されず、メモリ リークが発生していると不平を言っているようです!= NULL
。
コードは次のとおりです。
#include <stdio.h>
struct FileStructure {
/*@only@*/ FILE *file;
};
static /*@noreturn@*/ void die(const char *message)
{
if ((bool) errno) {
perror(message);
} else {
printf("ERROR: %s\n",message);
}
exit(EXIT_FAILURE);
}
static struct FileStructure *File_open(const char *filename)
{
struct FileStructure *filestruct = malloc(sizeof(struct FileStructure));
if(filestruct == NULL) die("Memory error");
filestruct->file = fopen(filename,"r+");
if(!filestruct->file) die("Failed to open the file");
return filestruct;
}
static void File_close(/*@only@*/ struct FileStructure *filestruct)
{
if(filestruct) {
if(filestruct->file != NULL ) (void) fclose(filestruct->file);
free(filestruct);
}
}
int main(int argc, char *argv[])
{
struct FileStructure *filestruct;
char *filename;
if(argc < 1) die("USAGE: program <filename>");
filename=argv[1];
filestruct=File_open(filename);
File_close(filestruct);
return 0;
}
そして、それは次のエラーを引き起こします:
so-splint-fclose.c: (in function File_open)
so-splint-fclose.c:22:3: Dependent storage assigned to only:
filestruct->file = fopen(filename, "r+")
Dependent storage is transferred to a non-dependent reference. (Use
-dependenttrans to inhibit warning)
so-splint-fclose.c: (in function File_close)
so-splint-fclose.c:32:10: Only storage filestruct->file (type FILE *) derived
from released storage is not released (memory leak): filestruct
A storage leak due to incomplete deallocation of a structure or deep pointer
is suspected. Unshared storage that is reachable from a reference that is
being deallocated has not yet been deallocated. Splint assumes when an object
is passed as an out only void pointer that the outer object will be
deallocated, but the inner objects will not. (Use -compdestroy to inhibit
warning)
2 番目のエラー:なぜ splintは が経由されているのにfilestruct->file
が閉じられていないと考えるのですか?File_close
fclose