本文介绍了std :: vector-擦除时崩溃(析构函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我希望您可能要向我解释为什么我的应用程序在std :: vector擦除时崩溃.
因此,让我向您介绍我的代码:

我有数据包流.我定义了设置有关对象信息的数据包.
然后是该对象的类:

CObject的描述:
CObject-
它具有3个指向堆的成员变量.这些变量中的两个是可选的.
(我将为这些变量使用一些随机名称,这并不重要)
m_chObjectName
m_chObjectDescriptor
m_chObjectSomething

事实:
CObject具有将这些指针设置为零的构造函数.

CObject具有复制构造函数,该构造函数:
a)将目标对象的指针设置为零
b)检查源对象的指针是否不为零(由于可选性)

Hello once again,

I was hoping you might want to explain me why my application crashes on std::vector erase.
So let me introduce you into my code:

I have packet stream. I defined the packet where the informations about object are set.
Then there is class for this object:

Description of CObject:
CObject -
It has 3 member variables which points to the heap. Two of those variables are optional.
(I will use some random names for those variables, it doesn''t really matter)
m_chObjectName
m_chObjectDescriptor
m_chObjectSomething

Facts:
The CObject has constructor which sets these pointers to zero.

The CObject has copy constructor which:
a) Sets the pointers of destination object to zero
b) Checks the pointers of source object if they are not zero (Because of optionality)

if( ObjectSource.m_chObjectName != 0)
  //copy objectSource.m_chObjectName into newly allocated memory for this->m_chObjectName;


c)复制其余变量.
this-> m_nID = ObjectSource.m_nID


CObject具有其成员方法,该方法将数据包的缓冲区作为参数,并将该类的所有相应变量设置为缓冲区中指定的值.方法的签名无效CObject :: SetObjectByPacket(unsigned char * byObjectPacket)

对象析构函数:
a)检查指针是否== 0,如果不删除,则将其删除


c) Copies rest of the variables.
this->m_nID = ObjectSource.m_nID
etc.

The CObject has it''s member method which takes buffer of packet as parameter and sets all corresponding variables of the class to the values specified in the buffer. The signature of method is void CObject::SetObjectByPacket(unsigned char * byObjectPacket)

The object destructor:
a) Checks if pointer == 0 and if not delete it

	if(m_chObjectName)
	{
		delete [] m_chObjectName;
		m_chObjectName = 0;
	}
//etc.



CObjectWrapper的描述
此类类似于CObject的接口类

它具有一个std :: vector< cobject>成员变量,称为m_Objects.
它有2种成员方法:
a)



Description of CObjectWrapper
This class is like interface class for CObject

It has one std::vector <cobject> member variable called m_Objects.
It has 2 member methods:
a)

void CObjectWrapper::InsertObjectFromPacket(unsigned char * byDataBuffer)
{
	CObject newObject;

	newObject.SetObjectByPacket(byDataBuffer);

	m_Objects.push_back(newObject);
}


b)


b)

void CObjectWrapper::DeleteObjectByID( unsigned long ulObjectID ) // ID is obtained from packet
{
	int nSize = m_Objects.size();

	for(int i = 0; i < nSize; i++)
	{
		if(m_Objects[i].GetObjectID() == ulObjectID) // GetObjectID return ID of CObject class
		{
			m_Objects.erase(m_Objects.begin()+(i));

			break;
		}
	}
}






那么问题出在哪里:
一段时间后,应用程序崩溃
m_Objects.erase(m_Objects.begin()+(i));
同时调用该CObject的析构函数.该CObject的堆指针有些奇怪,但肯定不是零.我可以通过为这些名称和描述符使用静态大小来绕过它,但这使我很烦,所以我想知道答案.

谢谢您的帮助.






So where is the problem:
The application crashes after some time on
m_Objects.erase(m_Objects.begin()+(i));
while calling destructor of that CObject. The heap pointers of that CObject are something strange but certainly not zero. I could bypass that by using static size for those names and descriptors, but this bugs me so I want to know the answer.

Thank you for you help.

推荐答案


这篇关于std :: vector-擦除时崩溃(析构函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 03:46