help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Should I free yyptr after parsing?


From: Robert Mosolgo
Subject: Should I free yyptr after parsing?
Date: Fri, 14 Apr 2023 09:41:27 -0400

Hi!

I'm writing my first Bison parser and, although it *works*, valgrind
reports a memory leak. For background, the grammar was first written for
"racc", Ruby's yacc-like parser generator, and I'm using it to parse Ruby
strings into a Ruby AST -- but via Ruby's C extension API. The lexer is
written with Ragel (also ported from Ruby).

The full parser definition is here:
https://github.com/rmosolgo/graphql-ruby/blob/a048682e6d8468d16947ee1c80946dd23c5d91f9/graphql-c_parser/ext/graphql_c_parser_ext/parser.y

And the generated parser is here:
https://github.com/rmosolgo/graphql-ruby/blob/a048682e6d8468d16947ee1c80946dd23c5d91f9/graphql-c_parser/ext/graphql_c_parser_ext/parser.c

And valgrind's error message says this:


200,014 bytes in 2 blocks are definitely lost in loss record 139,468 of 139,600
  malloc (vg_replace_malloc.c:393)
 *yyparse (parser.c:1768)
 *GraphQL_CParser_Parser_c_parse (graphql_c_parser_ext.c:8)
  vm_call_cfunc_with_frame (vm_insnhelper.c:3268)
  vm_sendish (vm_insnhelper.c:5080)
  vm_exec_core (insns.def:820)
  rb_vm_exec (vm.c:2374)


Which points to this part of my generated C code:

# else /* defined YYSTACK_RELOCATE */
      /* Extend the stack our own way.  */
      if (YYMAXDEPTH <= yystacksize)
        YYNOMEM;
      yystacksize *= 2;
      if (YYMAXDEPTH < yystacksize)
        yystacksize = YYMAXDEPTH;

      {
        yy_state_t *yyss1 = yyss;
        union yyalloc *yyptr =
          YY_CAST (union yyalloc *,
                   YYSTACK_ALLOC (YY_CAST (YYSIZE_T, YYSTACK_BYTES
(yystacksize))));
        if (! yyptr)
          YYNOMEM;
        YYSTACK_RELOCATE (yyss_alloc, yyss);
        YYSTACK_RELOCATE (yyvs_alloc, yyvs);#  undef YYSTACK_RELOCATE
        if (yyss1 != yyssa)
          YYSTACK_FREE (yyss1);
      }

Can you help me figure out what to do next? I see the allocation, but
I'm not sure if (or how) I might free that memory when my parser is
done. I've seen some references online to flex's `yydestroy()`
function, but I dont think that applies here since I'm not using flex.
Is there an alternative approach I might use to clean this up?

Thanks for taking a look! I'd appreciate any pointers in debugging if
anything comes to mind. I've also posted this question to Stack
Overflow: 
https://stackoverflow.com/questions/75996973/how-to-free-yyptr-stack-in-bison?noredirect=1#comment134038116_75996973

Best,

Robert


reply via email to

[Prev in Thread] Current Thread [Next in Thread]