我似乎有一點向量內存泄漏問題。 我的代碼如下所示:指向與矢量內存泄漏的對象
class CPart {
public:
virtual void print() = 0;
};
//some other classes
class CDisk : public CPart {
public:
CDisk(int tp, int size);
~CDisk();
virtual void print();
void AddPartition(int size, const string & dsc);
static const int MAGNETIC = 0;
static const int SDD = 1;
private:
struct CPartition {
CPartition(int size, const string & dsc);
int div_size;
string disk;
};
int type;
int d_size;
vector<CPartition> ptts;
};
CDisk::CDisk(int tp, int size) {
type = tp;
d_size = size;
}
CDisk::CPartition::CPartition(int size, const string& dsc) {
div_size = size;
disk = dsc;
}
void CDisk::AddPartition(int size, const string& dsc) {
ptts.push_back(CPartition(size, dsc));
}
int main(int argc, char** argv) {
CDisk disk(CDisk::SDD, 5000);
disk.AddPartition(500, "disk1");
CPart *disk2 = new CDisk(disk);
delete disk2;
return 0;
}
當我運行此代碼Valgrind的,它說有內存泄漏和丟失的字節數是向量的PTT * CPartition的大小等於項目的數量。所以我猜測我必須以某種方式清理那個向量。我嘗試過,但無濟於事。
謝謝,這真的幫了我很多。 – user2274361