您是否考虑过将这些对象存储在向量中,而不是指针中的?这将为每个对象节省四个字节 (即40Mb),使您的编码工作变得更加容易。 另外,从技术上讲,你应该在删除之前从 向量中删除指针。如果在 中删除指针它位于向量中,则向量包含一个不确定的 值,这可能会导致未定义的行为(即使 你对向量做的唯一事情是明确的)。 例如: for(vector< m_op *> :: iterator it = mVec.begin(); it!= mVec.end(); it ++) { m_op * ptr = 0; std :: swap(* it,ptr); 删除ptr; } mVec.clear (); 另请注意,向量上的clear()不需要释放向量使用的内存。要做到这一点,你必须摧毁向量的;一种方便的方法是: mVec.swap(std :: vector< m_op *>()); Have you considered storing the objects in the vector, ratherthan pointers? This will save you four bytes per object(ie. 40Mb), and makes your job as a coder a lot easier. Also, technically you should remove a pointer from thevector before you delete it. If you delete a pointer whileit is in a vector, then the vector contains an indeterminatevalue, and this could cause undefined behaviour (even ifthe only thing you do to the vector is clear it). For example:for (vector<m_op*>::iterator it = mVec.begin();it != mVec.end(); it++){m_op *ptr = 0;std::swap(*it, ptr);delete ptr;}mVec.clear(); Also note that clear() on a vector isn''t required to freethe memory used by the vector. To do that, you have to destroythe vector; one convenient way to do that is: mVec.swap( std::vector<m_op*>() ); 这篇关于请帮助:无法释放堆对象的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 15:03