Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best strategy for manual memory management using smart pointers?

Tags:

c++

c++11

Is there a recommended strategy for dealing with external libraries which expect manually managed raw pointers. For example a method which takes a vector of pointers:

ALibraryFunc(std::vector<ALibraryData*> p);

so generally you'd create your vector with something like:

std::vector<ALibraryData*> myVec;
for(...)
{
    myVec.push_back(new ALibraryData(args));
}
//and then
ret = ALibraryFunc(myVec);
//and then
for(auto &a:myVec)
{
    delete a;
}
myVec.clear();

I'd much prefer to use smart pointers but the library will not take them. This leaves me wondering if something like this is more stinky 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());
}
//and then
ret = ALibraryFunc(myVec);
myVec.clear();
myVecUP.clear();

The second version seems safer in case of exceptions or mistakes but it does risk dangling pointers. Am i missing something obvious here? What do/would you do?

like image 554
jacksawild Avatar asked Feb 13 '23 09:02

jacksawild


1 Answers

You can declare a small class which will own the vector of raw pointers, and delete the pointers in the destructor:

struct VecOwner {
 std::vector<ALibraryData*> vec;
 VecOwner(<some arguments>)
 {
     // fill the vector here
 }
 ~VecOwner()  
 {
   for(auto &a:vec)
     delete a;
   vec.clear();
 }
};

you should be able to reuse the class in all places where you use the library.

like image 152
Wojtek Surowka Avatar answered Feb 15 '23 10:02

Wojtek Surowka