Octave: remove allocation of new octave_value in SWIG_Octave_SetGlobalValue()

- this introduces a memory leak, which becomes significant for large
  modules (many global variables) and many module re-loadings (e.g.
  during a long-running script)
- the original motivation was to prevent double-frees on exit, but this
  problem appears to have been fixed by the _Exit() hack in later commits,
  and in any case is an issue only for Octave ~3.2, so it should be safe to
  remove; tested by running Octave examples/test suite with Debian 3.2.4 and
  built-from-source 3.2.4, 3.4.3, and 3.6.3
This commit is contained in:
Karl Wette 2013-05-09 14:35:41 +02:00 committed by William S Fulton
commit ef160ec072

View file

@ -1289,16 +1289,7 @@ SWIGRUNTIMEINLINE octave_value SWIG_Octave_GetGlobalValue(std::string name) {
}
SWIGRUNTIME void SWIG_Octave_SetGlobalValue(std::string name, const octave_value& value) {
// It is critical that a newly-allocated octave_value is passed to set_global_value(),
// since it and the Octave symbol table take references to the values assigned to it.
// If we were to pass a reference to 'value' to set_global_value(), then the Octave
// symbol table would hold a reference to a variable owned by the SWIG .oct module.
// Both will think that they own the reference (since the .oct module is dynamically
// loaded, it appears to have its own C++ runtime), and so they will both try to
// de-allocate the octave_value on exit, resulting in a double-free or seg-fault.
// This is prevented by giving Octave its own heap-allocated copy of 'value'.
octave_value *pov = new octave_value(value);
set_global_value(name, *pov);
set_global_value(name, value);
}
SWIGRUNTIME void SWIG_Octave_LinkGlobalValue(std::string name) {