Re: returning std::string by value across DLL boundaries
On Jun 9, 12:12 am, Le Chaud Lapin <jaibudu...@gmail.com> wrote:
On Jun 8, 12:16 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
[snip]> The problem is caused by the mismatch of allocation and deallocation
functions (new/delete, malloc/free) from different C++ run-time. To
solve it, you need to make sure that objects are allocated and
deallocated by functions from the same C++ run-time. The easiest way
to do so it to allocate and deallocate objects in the same source
file.
[snip]
A simple way to achieve this is to use objects with virtual
destructors.
There are several issues with such an approach.
One is that it is not applicable to std:: classes because most of them
don't have virtual destructors.
Another one, is that it is a Microsoft-specific hack. I.e. nothing in
the standard guarantees that the deallocation function will be called
from the same translation unit as the unit where the virtual
destructor is defined. (speculation: a virtual destructor could
internally return a pointer to the beginning of the object storage,
which delete expression would pass to operator delete() from the
current translation unit).
Instead, the desired effect can be achieved using standard and
portable means:
* Custom allocator for std:: containers.
* Class-specific overloaded new/delete, no virtual destructor
required, works well with smart-pointers.
* Factory functions matched with custom destroy function (i.e. Foo*
fooCreate() / void fooDestroy(Foo*)), works well with smart-pointers.
--
Max
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]