我有一个 unique_ptrs 向量到使用基类的派生类型存储
std::unique_ptr<std::vector<std::unique_ptr<Variable>>> decisionVariables;
其中 Variable 是超类,派生类型是 Route 类。我的问题是当包含决策变量的类被删除时,路由实例似乎没有被删除。
Route 派生自变量:
#ifndef __VARIABLE__
#define __VARIABLE__
/**
* Interface for decision variables.
*/
#include <cstring>
#include <ostream>
#include <memory>
class Variable {
public:
/**
* Returns an independent copy of this decision variable.
*
* @ret a copy of this decision variable
*/
virtual std::unique_ptr<Variable> copy () = 0;
virtual std::string toString () = 0;
};
#endif
路由的头文件:
#ifndef __ROUTE__
#define __ROUTE__
#include <vector>
#include <map>
#include <cstring>
#include <sstream>
#include <ostream>
#include <memory>
#include <set>
#include <algorithm>
#include "../../../Framework/h/core/Variable.h"
class Route : public Variable {
private:
std::unique_ptr<std::vector<int>> route;
double frequency;
double routeLength;
public:
Route ();
void add (int);
void addToFront (int);
void remove ();
void removeFromFront ();
std::vector<int>::iterator begin();
std::vector<int>::iterator end();
int size ();
std::vector<int> getViableNodes (std::shared_ptr<std::map<int, std::unique_ptr<std::vector<int>>>>, int);
int front ();
int back ();
std::string toString ();
int get (int);
bool containsLink (int, int);
bool contains (int);
void replace (int, int);
void setFrequency (double);
double getFrequency ();
void setRouteLength (double);
double getRouteLength ();
std::unique_ptr<Variable> copy ();
};
#endif
有没有办法防止目前经历的严重内存泄漏?