Although it seems to be a very common issue, I did not harvest much information: How can I create a safe interface between DLL boundaries regarding memory alloction?
It is quite well-known that
// in DLL a
DLLEXPORT MyObject* getObject() { return new MyObject(); }
// in DLL b
MyObject *o = getObject();
delete o;
might certainly lead to crashes.
But since interactions like the one above are - as I dare say - not uncommon, there has to be a way to ensure safe memory allocation.
Of course, one could provide
// in DLL a
DLLEXPORT void deleteObject(MyObject* o) { delete o; }
but maybe there are better ways (e.g. smart_ptr?). I read about using custom allocators when dealing with STL containers as well.
So my inquiry is more about general pointers to articles and/or literature dealing with this topic. Are there special fallacies to look out for (exception handling?) and is this problem limited to only DLLs or are UNIX shared objects "inflicted" too?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…