Fix bugs in Octave module loading

- fix a memory leak in setting of global variables
- install functions only once, to speed up module loads

See the git commit messages for further details.

Patches was tested by running Octave examples/test suite for versions 3.2.4, 3.4.3, 3.6.3