<分区>
我无法调用 delete[]
,因为它会在析构函数中引发错误。代码下面有注释。
class Packet
{
public:
Packet(char* data, int len, ConnectionData* connectiont, int cmdtype) : _len(len), _connection(connectiont), _cmdtype(cmdtype)
{
_data = new char[len]; //allocate memory
memcpy(_data, data, len); //copy to allocated memory
}
Packet() = default;
~Packet()
{
delete[] _data; /*error here*/
}
private:
char* _data;
int _len, _cmdtype = 0;
ConnectionData* _connection;
};
//Where create object of class and call constuctor.
char* _tempbuffer = new char[_templen];
memcpy(_tempbuffer, _ph.getBuffer() + _temppos, _templen);
_separatedPackets.push_back(Packet(_tempbuffer, _templen, connection, _cmdtype));