I'm wondering about a situation where I'm trying to return a vector from DLL to .exe app. Compilers and settings for DLL and .exe app are the same. I know that passing STL vector through DLL boundaries might cause memory errors in case of alloc/dealloc memory.
What about the case when returning a vector
wrapped by shared_ptr
? Is this memory safe?
Little example:
exported DLL method:
__declspec(dllexport) std::shared_ptr<std::vector<MyObject>> MyDLL::myMethod()
{
//Create object
MyObject obj;
std::vector<MyObject> myVector;
myVector.push_back(obj);
//Create wrapper
std::shared_ptr<std::vector<MyObject>> spvObject = std::make_shared<std::vector<MyObject>>(myVector);
return spvObject;
}
Get this data on .exe side:
MyDll dll;
std::shared_ptr<std::vector<MyObject>> objFromDll = dll.myMethod();
What will happen when pointer from .exe app will go out of scope? Will there be a memory error in case of deleting heap from DLL?