mardi 21 juillet 2015

Memory leak when using std::shared_ptr to std::vector

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)

Aucun commentaire:

Enregistrer un commentaire