- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个为了引入更大解决方案中的问题而创建的示例。我必须使用 flex 和 yy_scan_string() 。我在 Flex 中遇到内存泄漏问题(代码如下)。在此示例中,内存泄漏被标记为“仍然可访问”,但在原始解决方案中,它们被标记为“丢失的内存”。
我认为这个问题是由flex内部分配的内存中的某个地方,我不知道如何正确释放它,并且我找不到该问题的任何教程/文档。
%option noyywrap
%{
#include <stdio.h>
%}
%%
. printf("%s\n", yytext);
%%
int main() {
printf("Start\n");
yy_scan_string("ABC");
yylex();
printf("Stop\n");
return 0;
}
bash$ flex file.lex
bash$ gcc lex.yy.c
bash$ ./a.out
Start
H
a
l
l
o
W
o
r
l
d
Stop
bash$ valgrind --leak-check=full --show-leak-kinds=all ./a.out
==6351== Memcheck, a memory error detector
==6351== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6351== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==6351== Command: ./a.out
==6351==
==6351== error calling PR_SET_PTRACER, vgdb might block
Start
A
B
C
Stop
==6351==
==6351== HEAP SUMMARY:
==6351== in use at exit: 77 bytes in 3 blocks
==6351== total heap usage: 4 allocs, 1 frees, 589 bytes allocated
==6351==
==6351== 5 bytes in 1 blocks are still reachable in loss record 1 of 3
==6351== at 0x483577F: malloc (vg_replace_malloc.c:299)
==6351== by 0x10AE84: yyalloc (in ./a.out)
==6351== by 0x10ABE5: yy_scan_bytes (in ./a.out)
==6351== by 0x10ABBC: yy_scan_string (in ./a.out)
==6351== by 0x10AEE2: main (in /home/./a.out)
==6351==
==6351== 8 bytes in 1 blocks are still reachable in loss record 2 of 3
==6351== at 0x483577F: malloc (vg_replace_malloc.c:299)
==6351== by 0x10AE84: yyalloc (in ./a.out)
==6351== by 0x10A991: yyensure_buffer_stack (in ./a.out)
==6351== by 0x10A38D: yy_switch_to_buffer (in ./a.out)
==6351== by 0x10AB8E: yy_scan_buffer (in ./a.out)
==6351== by 0x10AC68: yy_scan_bytes (in ./a.out)
==6351== by 0x10ABBC: yy_scan_string (in ./a.out)
==6351== by 0x10AEE2: main (in ./a.out)
==6351==
==6351== 64 bytes in 1 blocks are still reachable in loss record 3 of 3
==6351== at 0x483577F: malloc (vg_replace_malloc.c:299)
==6351== by 0x10AE84: yyalloc (in ./a.out)
==6351== by 0x10AAEF: yy_scan_buffer (in ./a.out)
==6351== by 0x10AC68: yy_scan_bytes (in ./a.out)
==6351== by 0x10ABBC: yy_scan_string (in ./a.out)
==6351== by 0x10AEE2: main (in ./a.out)
==6351==
==6351== LEAK SUMMARY:
==6351== definitely lost: 0 bytes in 0 blocks
==6351== indirectly lost: 0 bytes in 0 blocks
==6351== possibly lost: 0 bytes in 0 blocks
==6351== still reachable: 77 bytes in 3 blocks
==6351== suppressed: 0 bytes in 0 blocks
==6351==
==6351== For counts of detected and suppressed errors, rerun with: -v
==6351== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
最佳答案
flex文档中提到了答案,但是你必须仔细阅读才能找到它。 (见下文。)
如果您生成可重入扫描仪,则您负责创建和销毁所需的每个扫描仪对象,并且扫描仪对象管理扫描仪实例所需的所有内存。但即使不使用可重入接口(interface),也可以使用yylex_destroy
来管理内存。在传统的不可重入接口(interface)中,没有 scanner_t
参数,因此 yylex_destroy
的原型(prototype)很简单
int yylex_destroy(void);
(虽然它有一个返回值,应该是状态代码,但它永远不会返回错误。)
如果您愿意,您可以调用yylex_init
,它在不可重入接口(interface)中也不接受任何参数,但与可重入接口(interface)不同的是,没有必要调用它。
摘自手册memory management章节:
<小时/>Flex allocates dynamic memory during initialization, and once in a while from within a call to yylex(). Initialization takes place during the first call to yylex(). Thereafter, flex may reallocate more memory if it needs to enlarge a buffer. As of version 2.5.9 Flex will clean up all memory when you call yylex_destroy See faq-memory-leak.
示例:
$ cat clean.l
%option noinput nounput noyywrap nodefault
%{
#include <stdio.h>
%}
%%
.|\n ECHO;
%%
int main() {
printf("Start\n");
yy_scan_string("ABC");
yylex();
printf("Stop\n");
yylex_destroy();
return 0;
}
$ flex -o clean.c clean.l
$ gcc -Wall -o clean clean.c
$ valgrind --leak-check=full --show-leak-kinds=all ./clean
==16187== Memcheck, a memory error detector
==16187== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==16187== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==16187== Command: ./clean
==16187==
Start
ABCStop
==16187==
==16187== HEAP SUMMARY:
==16187== in use at exit: 0 bytes in 0 blocks
==16187== total heap usage: 4 allocs, 4 frees, 1,101 bytes allocated
==16187==
==16187== All heap blocks were freed -- no leaks are possible
==16187==
==16187== For counts of detected and suppressed errors, rerun with: -v
==16187== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
关于c - Flex yy_scan_string 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59522969/
我使用flex制作了一个词法分析器。我想分析一些定义编译器语句,其格式为:#defineidentifieridentifier_string。我保留了(标识符identifier_string)对的
描述 这是一个为了引入更大解决方案中的问题而创建的示例。我必须使用 flex 和 yy_scan_string() 。我在 Flex 中遇到内存泄漏问题(代码如下)。在此示例中,内存泄漏被标记为“仍然
我想解析一个字符串,我在 yacc 的 main 函数中提供给解析器。我知道这可以通过使用 yy_scan_string 来完成,但我不知道如何使用它。我搜索了网络和手册页,但我仍然不清楚。请帮助我。
下面的 flex 代码似乎创建了一个可执行文件,当输入字符串是一个未终止的注释时会出现段错误。请注意: 这仅在输入缓冲区是字符串(例如使用 yy_scan_string())而不是文件(yyset_i
我想在我的 main.cpp 中使用 YY_BUFFER_STATE yy_scan_string(const char *str) 和其他函数,如 yyparse(),我做的事情: extern "
我是一名优秀的程序员,十分优秀!