I read " Using shared_ptr in dll interfaces ". In this article, phlipsy suggested a way to pass a specific implementation object across the boundaries of a DLL at the end of its answer. Basically, the idea is to return the raw pointer from the DLL and initialize shared_ptr
to the EXE w / raw raw pointer.
I do not think this is correct. Let me reprogram it for simplicity.
// wrong version?? // DLL Object* createObject() { return new Object; } // EXE std::tr1::shared_ptr<Object> p(createObject()); ..
When an object
freed, the destruction context / heap used by shared_ptr
is different from that used in the DLL at build time.
The correct way to use shared_ptr
is to allocate resources on the same line with shared_ptr
initialization so that allocation and release can use the same heap, as shown below.
// right version // DLL std::tr1::shared_ptr<Object> createObject() { return std::tr1::shared_ptr<Object>(new Object); } // EXE std::tr1::shared_ptr<Object> p(createObject()); ..
I'm right?
source share