Is there a recommended strategy for working with external libraries that expect manual guided pointers. For example, a method that takes a pointer vector:
ALibraryFunc(std::vector<ALibraryData*> p);
so usually you create your vector with something like:
std::vector<ALibraryData*> myVec;
for(...)
{
myVec.push_back(new ALibraryData(args));
}
ret = ALibraryFunc(myVec);
for(auto &a:myVec)
{
delete a;
}
myVec.clear();
I would prefer to use smart pointers, but the library will not take them. This makes me wonder if something like this is more smelly than just doing it manually:
std::vector<std::unique_ptr<ALibraryData>> myVecUP;
std::vector<ALibraryData*> myVec;
for(...)
{
myVecUP.push_back(std::make_unique<ALibraryData>(args));
myVec.push_back(myVecUP.back().get());
}
ret = ALibraryFunc(myVec);
myVec.clear();
myVecUP.clear();
The second version seems safer in case of exceptions or errors, but it risks dangling pointers. Am I missing something here? What are you doing / will do?
source
share