[GiNaC-devel] [PATCH] [BUGFIX] Reclaiming the memory allocated for static objects *is* necessary.
Alexei Sheplyakov
varg at theor.jinr.ru
Tue Aug 19 15:16:00 CEST 2008
GiNaC allocates memory for static objects (i.e. flyweights, remember tables,
etc), but doesn't free it. This is OK if the program lifetime matches libginac
lifetime, since the OS will reclaim that memory anyway.
However, if the program lifetime is different from that of libginac, this
turns into a memory leak. This happens if someone dlopen's libginac.so, and
dlclose's it later on (read: if someone uses GiNaC via scripting language
bindings).
symbol::autoname_prefix(): there's no need for dynamical memory allocation.
remember_table::remember_tables(): likewise.
function::registered_functions(): likewise.
lib_init::~lib_init(): if library usage count drops to 0, reclaim the memory
allocated for flyweights.
---
ginac/function.pl | 4 +-
ginac/remember.cpp | 4 +-
ginac/symbol.cpp | 6 ++--
ginac/utils.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++--
4 files changed, 59 insertions(+), 10 deletions(-)
diff --git a/ginac/function.pl b/ginac/function.pl
index 3e44735..7a4d5e7 100644
--- a/ginac/function.pl
+++ b/ginac/function.pl
@@ -1330,8 +1330,8 @@ ${power_switch_statement}
std::vector<function_options> & function::registered_functions()
{
- static std::vector<function_options> * rf = new std::vector<function_options>;
- return *rf;
+ static std::vector<function_options> rf = std::vector<function_options>();
+ return rf;
}
bool function::lookup_remember_table(ex & result) const
diff --git a/ginac/remember.cpp b/ginac/remember.cpp
index faa89d3..701a27e 100644
--- a/ginac/remember.cpp
+++ b/ginac/remember.cpp
@@ -183,8 +183,8 @@ void remember_table::init_table()
std::vector<remember_table> & remember_table::remember_tables()
{
- static std::vector<remember_table> * rt = new std::vector<remember_table>;
- return *rt;
+ static std::vector<remember_table> rt = std::vector<remember_table>();
+ return rt;
}
} // namespace GiNaC
diff --git a/ginac/symbol.cpp b/ginac/symbol.cpp
index f1b2be6..e56ade3 100644
--- a/ginac/symbol.cpp
+++ b/ginac/symbol.cpp
@@ -340,10 +340,10 @@ void symbol::unassign()
/** Symbols not constructed with a string get one assigned using this
* prefix and a number. */
-std::string & symbol::autoname_prefix()
+std::string& symbol::autoname_prefix()
{
- static std::string *s = new std::string("symbol");
- return *s;
+ static std::string s("symbol");
+ return s;
}
/** Return default TeX name for symbol. This recognizes some greek letters. */
diff --git a/ginac/utils.cpp b/ginac/utils.cpp
index b55948b..f44ed9f 100644
--- a/ginac/utils.cpp
+++ b/ginac/utils.cpp
@@ -388,9 +388,58 @@ library_init::library_init()
library_init::~library_init()
{
if (--count==0) {
- // In theory, we would have to clean up here. But since we were
- // only initializing memory in the ctor and that memory is reclaimed
- // anyways by the OS when the program exits, we skip this.
+ // It's really necessary to clean up, since the program
+ // lifetime might not be the same as libginac.{so,dll} one
+ // (e.g. consider // dlopen/dlsym/dlclose sequence).
+ delete _num120_p;
+ delete _num_120_p;
+ delete _num60_p;
+ delete _num_60_p;
+ delete _num48_p;
+ delete _num_48_p;
+ delete _num30_p;
+ delete _num_30_p;
+ delete _num25_p;
+ delete _num_25_p;
+ delete _num24_p;
+ delete _num_24_p;
+ delete _num20_p;
+ delete _num_20_p;
+ delete _num18_p;
+ delete _num_18_p;
+ delete _num15_p;
+ delete _num_15_p;
+ delete _num12_p;
+ delete _num_12_p;
+ delete _num11_p;
+ delete _num_11_p;
+ delete _num10_p;
+ delete _num_10_p;
+ delete _num9_p;
+ delete _num_9_p;
+ delete _num8_p;
+ delete _num_8_p;
+ delete _num7_p;
+ delete _num_7_p;
+ delete _num6_p;
+ delete _num_6_p;
+ delete _num5_p;
+ delete _num_5_p;
+ delete _num4_p;
+ delete _num_4_p;
+ delete _num3_p;
+ delete _num_3_p;
+ delete _num2_p;
+ delete _num_2_p;
+ delete _num1_p;
+ delete _num_1_p;
+ delete _num1_2_p;
+ delete _num_1_2_p;
+ delete _num1_3_p;
+ delete _num_1_3_p;
+ delete _num1_4_p;
+ delete _num_1_4_p;
+ delete _num0_p;
}
}
--
1.5.6
Best regards,
Alexei
--
All science is either physics or stamp collecting.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: Digital signature
Url : http://www.cebix.net/pipermail/ginac-devel/attachments/20080819/06dea2dc/attachment.sig
More information about the GiNaC-devel
mailing list