fix memory leak

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21898 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Peter Kümmel 2007-12-01 10:52:52 +00:00
parent b5158fbe51
commit 1afafb9d5b

View File

@ -18,6 +18,7 @@
#include "support/os.h" #include "support/os.h"
#include <boost/tokenizer.hpp> #include <boost/tokenizer.hpp>
#include <boost/shared_array.hpp>
#include <cstdlib> #include <cstdlib>
#include <map> #include <map>
@ -65,22 +66,17 @@ bool setEnv(string const & name, string const & value)
string const encoded = to_local8bit(from_utf8(value)); string const encoded = to_local8bit(from_utf8(value));
#if defined (HAVE_SETENV) #if defined (HAVE_SETENV)
return ::setenv(name.c_str(), encoded.c_str(), true); return ::setenv(name.c_str(), encoded.c_str(), true);
#elif defined (HAVE_PUTENV) #elif defined (HAVE_PUTENV)
static std::map<string, char *> varmap; static std::map<string, boost::shared_array<char> > varmap;
string envstr = name + '=' + encoded; string envstr = name + '=' + encoded;
char * newptr = new char[envstr.size() + 1]; boost::shared_array<char> newptr(new char[envstr.size() + 1]);
envstr.copy(newptr, envstr.length()); envstr.copy(newptr.get(), envstr.length());
newptr[envstr.length()] = '\0'; newptr.get()[envstr.length()] = '\0';
int const retval = ::putenv(newptr); bool const retval = ::putenv(newptr.get()) == 0;
char * oldptr = varmap[name];
if (oldptr)
delete oldptr;
varmap[name] = newptr; varmap[name] = newptr;
return retval == 0; return retval;
#else #else
#error No environment-setting function has been defined. #error No environment-setting function has been defined.
#endif #endif