When working with windows dll's, it is established that we should limit the memory allocation/deallocation within the dll boundary since the dll could be using its own heap. So we have export allocators and free functions to from dll.
IsomeInterface* getObject();
void freeObject(IsomeInterface *object);
This way the object creation and deletion will reside within the dll.
Does this problem also exist on shared libraries on linux? When dealing with shared libs (.so) do we also need to take care of keeping allocation/deallocation within the shared lib. I did some quick tryout below and it works on linux. The same example would not work with windows dll (it will work with windows dll if both exe and dll compiled with /MD to make use of the same heap).
std::vector<int> vec;
vec.push_back(42);
PassAVector(vec);
where PassAVector
resides in a shared lib
void PassAVector(std::vector<int> &vec)
{
vec.push_back(1); // These would cause reallocation
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
}
Does this mean shared libs on unix share the heap with the executables (equivalent to the /MD switch on windows)?
Is it possible to compile (some compiler flags) the shared lib (.so) or the executable on linux in such a way that they start using different heaps (/MT switch on windows) and this problem surfaces?
EDIT: Found this which seem to suggest passing STL across boundaries is okay as long long as the compiler is gcc.