Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commit-message: 释放未解析成语法树的id #479 #485

Merged
merged 3 commits into from
Nov 25, 2024

Conversation

LvNing-bwt
Copy link
Contributor

What problem were solved in this pull request?

当输入sql语句错误并exit后,会出现内存未释放错误
Issue Number: close #479

Problem:
在处理 SQL 解析时,如果 SQL 语句错误且调用 exit,程序未正确释放分配的内存,导致内存泄漏。

What is changed and how it works?

修改了lex_sql.l文件,跟踪了所有使用strdup复制的变量的地址,新写统一释放函数
修改了yacc_sql.y文件,在语法解析结束后调用释放函数,统一释放由strdup复制的变量

@@ -46,6 +47,7 @@ while (0);
extern int atoi();
extern double atof();

std::vector<char *> allocated_strings;
Copy link
Collaborator

@hnwyllmm hnwyllmm Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用全局变量的话,并发时会出现访问冲突。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那我选择在全局变量上加锁这种方式可以吗

使用全局变量的话,并发时会出现访问冲突。

@hnwyllmm
Copy link
Collaborator

hnwyllmm commented Nov 22, 2024

加锁只能解决访问冲突,但是一个线程会把另一个线程的数据给释放掉。应该每次仅记录当前请求的信息,在语法解析完成时释放掉。

@hnwyllmm
Copy link
Collaborator

我大概看了下lex的相关内容,这里有一个yyscan_t的结构,执行lex相关的函数时都会带上这个对象。可以看到sql_parse函数的定义:

int sql_parse(const char *s, ParsedSqlResult *sql_result) {
  yyscan_t scanner;
  yylex_init(&scanner);
  scan_string(s, scanner);
  int result = yyparse(s, sql_result, scanner);
  yylex_destroy(scanner);
  return result;
}

yyscan_t 是一个 void* 在lex_sql.h 中可以看到定义。
yylex_init 对scanner做初始化;
scan_string 传入了yyscan_t的参数。

可以在lex_sql.h中看到,除了一个yylex_init函数之外,还有这样一个函数:

int yylex_init_extra ( YY_EXTRA_TYPE user_defined, yyscan_t* scanner);

看起来就是除了lex本身需要的一些成员变量,我们还可以在scanner里面塞一些自己的信息,就是 user_defined
理论上就可以把你设置的全局变量,当成 user_defined 放在scanner中。而在sql_parse 中调用 yylex_init_extra 把自己的信息放进去。当然在yylex_destroy之前,需要把一些数据释放掉。

yylex_init_extra的注释在lex_sql.cpp 中:

/* yylex_init_extra has the same functionality as yylex_init, but follows the
 * convention of taking the scanner as the last argument. Note however, that
 * this is a *pointer* to a scanner, as it will be allocated by this call (and
 * is the reason, too, why this function also must handle its own declaration).
 * The user defined value in the first argument will be available to yyalloc in
 * the yyextra field.
 */

@hnwyllmm hnwyllmm merged commit 55c9fcd into oceanbase:main Nov 25, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]当输入sql语句错误并exit后,会出现内存未释放错误
2 participants