【发布时间】:2011-08-02 09:15:32
【问题描述】:
在我的代码中,我有一个类 lipid,其中包含三个 beads:
struct lipid{
particle mainPart;
bead * head;
bead * body;
bead * tail;
int LID;
vec direction;
bead * operator[](int index){
switch(index){
case 0: return head;
case 1: return body;
case 2: return tail;
default: return body;
}
}
};
struct bead{
particle mainPart;
int charge;
int type;
double rho;
double nextRho;
int LID;
double U;
double nextU;
bool touch;
};
struct particle{
vec pos;
vec oldPos;
vec vel;
vec oldVel;
vec F;
vec oldF;
};
class vec{
velarry<double> coor;
double x;
double y;
double z;
}
当我尝试创建脂质时,我使用 new 创建了三个珠子
lipid * l = new lipid;
l->head = new bead;
l->body = new bead;
l->tail = new bead;
当我对我的代码进行 valgrind 时,我收到一个错误,声称缺少大量块...我应该对此有多担心?我应该声明我正在将beads 和lipids 推入(几个)向量中。
编辑
好的,添加delete head .. 解决了这个问题,但我仍然有一个疼痛的问题,我有一条线:
this->beadBoxes[t[0]][t[1]][t[2]].push_back(b);
t 是大小为 3 的 vector<int>,beadsBoxes 是:
<vector<vector<vector<vector<bead*> > > > beadBoxes;
这家伙给我5次内存泄漏错误:
==22458== 48 bytes in 2 blocks are definitely lost in loss record 11 of 106
==22458== at 0x4A0666E: operator new(unsigned long) (vg_replace_malloc.c:220)
==22458== by 0x419A3C: __gnu_cxx::new_allocator<bead*>::allocate(unsigned long, void const*) (new_allocator.h:88)
==22458== by 0x419A64: std::_Vector_base<bead*, std::allocator<bead*> >::_M_allocate(unsigned long) (stl_vector.h:127)
==22458== by 0x423E1F: std::vector<bead*, std::allocator<bead*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<bead**, std:\
:vector<bead*, std::allocator<bead*> > >, bead* const&) (vector.tcc:275)
==22458== by 0x424073: std::vector<bead*, std::allocator<bead*> >::push_back(bead* const&) (stl_vector.h:610)
==22458== by 0x409664: membrane::updateBox(bead*) (membrane.cpp:874)
==22458== by 0x40ACA5: membrane::decide(lipid*) (membrane.cpp:793)
==22458== by 0x40DF01: membrane::rotate() (membrane.cpp:529)
==22458== by 0x40DFAF: membrane::MCstep() (membrane.cpp:480)
==22458== by 0x401B54: main (main.cpp:15)
我怀疑这可能与发生的分段错误有关,那就是那条线。为什么会出现 new (unsigned long) 以及为什么会引发分段错误?
【问题讨论】:
-
嗯 - 你要删除珠子 + 脂质吗?显然,如果你不删除它们,你会得到一个泄漏......
-
不,它们会一直保存到运行结束。当我只创建脂质(没有珠子)时,没有这样的信息
标签: c++ memory-leaks valgrind