Re: possible memory leak in Stream Handling

Daniel Diaz <[email protected]> Mon, 10 Sep 2012 15:53:10 +0200
Newsgroups gmane.comp.gnu.prolog.bugs
Message-ID <[email protected]>
Hi Mario,

could you send me a little example to reproduce the bug (with 
information about how to compile).
Also could you please explain me what you want to do (maybe there is a 
way do do it without strings).

Thanks

Daniel

Le 05/09/2012 21:13, Mario Frank a écrit :
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Dear GProlog-implementors,
>
> I am using GNU Prolog in combination with c++ (and compilation via gplc).
> Specifically, I call Prolog predicates by c++ to make an unifikation of
> two Terms and some other stuff.
> Since I don't know the structure of the two terms (arities of embedded
> Functions, etc.)
> I Transform them in c++ with Pl_Mk_Chars and reconstruct the terms in
> Prolog via
> open_input_chars_stream, read_term,close_input_chars_stream.
> In another used predicate, I transform a List to a String via atom_stream.
>
> I tested my System for memory leaks and found a lot leaks which seem to
> originate in GNU Prolog.
> I also tried using the prolog Predicates in the GNU Prolog interpreter.
> The leak is found there, too.
> So it can not originate in my c++-code.
>
> I start the Prolog Engine once via Pl_Start_Prolog and stop it via
> Pl_Stop_Prolog.
>
> Here is a snippet of the valgrind-output :
>
> ==4182== 571,649 bytes in 3,572 blocks are still reachable in loss
> record 2,008 of 2,008
> ==4182==    at 0x4C836CD: malloc (in
> /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
> ==4182==    by 0x593F801: strdup (strdup.c:43)
> ==4182==    by 0x4A7F40: Pl_Strdup_Check (misc.c:148)
> ==4182==    by 0x4ACFBD: Pl_Create_Allocate_Atom (atom.c:248)
> ==4182==    by 0x44BC20: Pl_Un_String_Check (c_supp.c:1941)
> ==4182==    by 0x48045A: Pl_Close_Output_Term_Stream_2 (stream_c.c:1503)
> ==4182==    by 0x47C1C5: ??? (in
> /home/eladrion/ATP/Templar/build/templar.bin)
> ==4182==    by 0x4A96F2: Call_Next (engine.c:449)
> ==4182==    by 0x4A9501: Pl_Call_Prolog (engine.c:352)
> ==4182==    by 0x44CB23: Pl_Query_Call (foreign_supp.c:323)
>
> This is the c++-code which calls the predicate:
>
>      Pl_Query_Begin (PL_TRUE);
>      PlTerm arg[2];
>      arg[0] = Pl_Mk_Chars(goal->c_str());
>      arg[1] = Pl_Mk_Variable();
>     
>      int result = Pl_Query_Call(Pl_Find_Atom("parseAx"), 2, arg);
>      char * term;
>      if (result){
>          term = Pl_Rd_String(arg[1]);
>          if (term == NULL){
>            Pl_Query_End(0);
>            return 1;
>          }
>          answer->append(std::string(term));
>         
>          Pl_Query_End(0);
>          return 0;
>         
>      }
>      Pl_Query_End(0);
>      return 1;
>
> Finally, this are the data conversion predicates I use:
>
> %%
> % Transforms a String (Chars stream) to a Prolog Term
> % templarStringToTerm(+String,-Term).
> %%
> templarStringToTerm(String,Term) :-
>      open_input_chars_stream(String, Stream),
> read_term(Stream,Term,[end_of_term(dot)]),
>      close_input_chars_stream(Stream),!.
>
> %%
> % Transforms a Prolog Object (List, Term,...) to a String
> % templarXToString(+Something,-String).
> %%
> templarXToString(Something,String) :-
>      open_output_atom_stream(Stream), writeq(Stream,Something),
>      flush_output(Stream), close_output_atom_stream(Stream,String),!.
>
> I already searched for Information in Mailing lists but did not find
> answers.
>
> Also, I viewed the GProlog-Code and found some suspicious lines here:
> Pl_Close_Output_Term_Stream_2 (stream_c.c:1503)
>
> The char * str is created. If the Unification (Pl_Un_Chars....) fails,
> the function returns false but
> does not free the memory.
>
> If this is really a bug, could you send me the spots which I need to fix
> on my system?
>
> I use gprolog 1.4.1 (1.4.0 has the same leak) and g++ 4.6.3 on Ubuntu
> 12.04 64 bit.
>
> Cheers, Mario
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://www.enigmail.net/
>
> iQEcBAEBAgAGBQJQR6RkAAoJEDnmHcItIfWElw4IAOVLMIpWgXRgnvvbKZHXsi9r
> 57+6UMXoA5KoS9NNV3Aa8qxfVAtCbuQ0RLnOGlnlnyBTrsTr9eNk2FBgAfSpInoe
> iPlsHKHo8SYGKWEb7uso4XsdxlS3gGEqV7SkDlS3YPAkEakNbteGyhQ0heDlYH14
> qLPh0VWuJXwN+/9bR6rNOC7LQiTBGbfV0efSENd0Ap+67Qg3gGH7e+5u2eB5n3Io
> WDSp8Jv8nTLtSEWZpxoc/ZlLrErV4KTHZVaOLxMnE/eRmgVmWEuu2WZwwBWxpd5a
> YFhH4SRxtDs4gI2n61edEVGqnh6lIrVhHYtCBYco9cfgO4ug1nyMqFbR3fcW4Qg=
> =JQbI
> -----END PGP SIGNATURE-----
>
>


-- 
Ce message a ete verifie par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a ete trouve.