]> www.ginac.de Git - ginac.git/blob - check/parser_memleak.cpp
pgcd(), chinrem_gcd(): use appropriate definition of the degree.
[ginac.git] / check / parser_memleak.cpp
1 /** @file parser_memleak.cpp
2  *
3  *  This small program exhibits the memory leak in the ginac_yylex().
4  *  Run it as
5  *
6  *  valgrind --leak-check=yes  ./parser_memleak
7  *
8  *  or simply
9  *
10  *  ulimit -v `expr 64 \* 1024` ./parser_memleak
11  */
12
13 /*
14  *  GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
15  *
16  *  This program is free software; you can redistribute it and/or modify
17  *  it under the terms of the GNU General Public License as published by
18  *  the Free Software Foundation; either version 2 of the License, or
19  *  (at your option) any later version.
20  *
21  *  This program is distributed in the hope that it will be useful,
22  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *  GNU General Public License for more details.
25  *
26  *  You should have received a copy of the GNU General Public License
27  *  along with this program; if not, write to the Free Software
28  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
29  */
30
31 #include <ginac/ginac.h>
32 using namespace GiNaC;
33
34 #include <iostream>
35 #include <stdexcept>
36 using namespace std;
37
38 int main(int argc, char** argv) {
39         const symbol x("x"), y("y");
40         const lst syms(x, y);
41         // parser-generated symbol => memory leak.
42         static const char* str[] = { "x^2+2*x*y + cos(x)", "Li2(x/y) + log(y/x)" };
43         
44         // depends on the amount of the available VM, compiler options, etc.
45         const unsigned N_max = 500000;
46         unsigned N=0;
47         ex e;
48         try {
49                 for (; N < N_max; N++) {
50                         e = ex(str[N & 1], syms);
51                 }
52         } catch (std::bad_alloc) {
53                 cerr << "N = " << N << endl;
54                 return 1;
55         }
56         return 0;
57 }