为什么yacc在退出时有内存泄漏?

时间:2016-10-24 20:38:49

标签: c memory-leaks bison yacc lex

当我用valgrind执行我的lex / yacc程序时,它发现内存泄漏。但我知道是否可以删除此内存泄漏。 valgrind的输出是:

\node_modules\browser-sync\node_modules\rx\ts\tsconfig.json

我的lex:

==10006== 
==10006== HEAP SUMMARY:
==10006==     in use at exit: 16,458 bytes in 3 blocks
==10006==   total heap usage: 13 allocs, 10 frees, 16,684 bytes allocated
==10006== 
==10006== 8 bytes in 1 blocks are still reachable in loss record 1 of 3
==10006==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10006==    by 0x4028FA: yyalloc (lex.yy.c:1852)
==10006==    by 0x402411: yyensure_buffer_stack (lex.yy.c:1552)
==10006==    by 0x400DA9: yylex (lex.yy.c:686)
==10006==    by 0x40312D: yyparse (parser.tab.c:1183)
==10006==    by 0x40462F: parse (Parser.c:10)
==10006==    by 0x4045EB: main (main.c:21)
==10006== 
==10006== 64 bytes in 1 blocks are still reachable in loss record 2 of 3
==10006==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10006==    by 0x4028FA: yyalloc (lex.yy.c:1852)
==10006==    by 0x401FA5: yy_create_buffer (lex.yy.c:1387)
==10006==    by 0x400DD3: yylex (lex.yy.c:688)
==10006==    by 0x40312D: yyparse (parser.tab.c:1183)
==10006==    by 0x40462F: parse (Parser.c:10)
==10006==    by 0x4045EB: main (main.c:21)
==10006== 
==10006== 16,386 bytes in 1 blocks are still reachable in loss record 3 of 3
==10006==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10006==    by 0x4028FA: yyalloc (lex.yy.c:1852)
==10006==    by 0x401FDC: yy_create_buffer (lex.yy.c:1396)
==10006==    by 0x400DD3: yylex (lex.yy.c:688)
==10006==    by 0x40312D: yyparse (parser.tab.c:1183)
==10006==    by 0x40462F: parse (Parser.c:10)
==10006==    by 0x4045EB: main (main.c:21)
==10006== 
==10006== LEAK SUMMARY:
==10006==    definitely lost: 0 bytes in 0 blocks
==10006==    indirectly lost: 0 bytes in 0 blocks
==10006==      possibly lost: 0 bytes in 0 blocks
==10006==    still reachable: 16,458 bytes in 3 blocks
==10006==         suppressed: 0 bytes in 0 blocks
==10006== 
==10006== For counts of detected and suppressed errors, rerun with: -v
==10006== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

我的yacc文件:

Chiffre [0-9]
Lettre [a-zA-Z]
Alphanum ({Chiffre}{Lettre})+

%{

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "parser.tab.h"

%}
%%
-[a-zA-Z]+ {
yylval.
string = strdup(yytext);
    return OPTION;
}

[a-zA-Z0-9_\-./]+ {
    yylval.string = strdup(yytext);
    return WORD;
}
"||" {
return OR;
}
"|" {
return PIPE;
}
"&&" {
return AND;
}
"&" {
    return AMPERSAND;
}
";" {
return SEMICOLON;
}
"2>" {
return
ERR_GREAT;
}

">&" {
return
GREAT_AMP;
}

"2>>" {
return
ERR_GREAT_GREAT;
}
">>&" {
return
GREAT_GREAT_AMP;
}
">>" {
return
GREAT_GREAT;
}

">" {
return
GREAT;
}

"<" {
return
LESS;
}

[ \t]+ {
}

"\n" {
return 0;
}

%%

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

  

== 10006 ==泄漏摘要:

     

== 10006 ==绝对丢失:0个块中的0个字节

     

== 10006 ==间接丢失:0个块中的0个字节

     

== 10006 ==可能丢失:0个块中的0个字节

     

== 10006 ==仍然可以访问:3个块中的16,458个字节

     

== 10006 ==抑制:0块中的0字节

对于大多数程序来说,不释放仍然可访问的内存是很正常的。由于在退出时仍然有一个指向它的指针,它实际上并不是泄漏。