I have a class consisting purely of class statics in one .so
, which gets called from various other .so
s, but on program exit under valgrind
I get a double free error that says:
==23162== Invalid free() / delete / delete[] / realloc()
...
==23162== Address 0xe187d70 is 0 bytes inside a block of size 97 free'd
...
==23162== Block was alloc'd at
....
==23162== by 0x165C0C90: FileManager::setCommonConfigFolder(...)
(in .../lib2.so)
The code is actually in lib1.so
, but called from lib2.so
.
Looking around, I see this answer:
No it is not shared - the code/text section of the library is shared - the data portion is unique to each process that uses the library
So what is the timing for freeing static objects allocated by the actions of the client of the library? How do I avoid this problem?
The relevant portions of the code are as follows. This is compiled into lib1.so
and called from lib2.so
and lib3.so
. With suitable logging, I can see that the address in the valgrind
report Address 0xe187d70 is 0 bytes inside a block of size 97 free'd
is indeed mCommonConfigDirectory.data
.
FileManager.cpp
#include "FileManager.h"
#include <string>
std::string FileManager::mCommonConfigDirectory = "../common/config";
void FileManager::setCommonConfigFolder (std::string const& configPath)
{
mCommonConfigDirectory = configPath;
}
FileManager.h
#include <string>
class FileManager
{
public:
static void setCommonConfigFolder (std::string const& configPath);
protected:
static std::string mCommonConfigDirectory;
};