I have an Image class :
class Image{
public:
Image()
{
vector_ptr = std::make_shared<std::vector<Feature>>(feature_vector);
}
std::shared_ptr<std::vector<Feature>> calculate_vector()
{
// iterates over a space of possible features, calling
// vector_ptr->push_back(Feature(type, i, j, w, h, value))
return vector_ptr;
}
std::shared_ptr<std::vector<Feature>> get_vector()
{
return vector_ptr;
}
void clear_vector()
{
vector_ptr->clear();
}
private:
std::vector<Feature> feature_vector;
std::shared_ptr<std::vector<Feature>> vector_ptr;
};
Where Feature is given by :
struct Feature
{
Feature(int type, int i, int j, int w, int h, double value);
void print();
int type;
int i, j;
int w, h;
double value;
};
But after successively calling calculate_vector(), and then clear_vector(), htop indicates there is a memory leak.
How do I resolve this problem? (The feature vector is very large)
vector_ptr
is a copy of feature_vector
, it isn't a shared wrapper around it. So you'd need to call feature_vector.clear();
as well if you wanted to free up its memory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.