1.先看源代码
class:
class Info
{
public:
Info(string a_strName)
{
_name = string(a_strName);
}
~Info()
{
if (NULL != _nbytes)
{
delete[] _nbytes;
_nbytes = NULL;
}
}
public:
int RspOnInfoContentSize()
{
if (NULL != _nbytes)
return _count;
GetRspOnInfoContent();
return RspOnInfoContentSize();
}
void* GetRspOnInfoContent()
{
if (NULL != _nbytes)
return _nbytes;
_count = sizeof(RspOnInfoContent) + _name.size();
_nbytes = new BYTE[_count];
auto& rspOnInfoContent = *(RspOnInfoContent*)_nbytes;
rspOnInfoContent.NameSize = _name.size();
memcpy(_nbytes + sizeof(RspOnInfoContent), _name.data(), _name.size());
return _nbytes;
}
private:
string _name;
private:
BYTE* _nbytes = NULL;
int _count = 0;
};
上面这个类Info附加了序列化的函数GetRspOnInfoContent()
为了效率方面的考虑,我们添加了_nbytes,来创建一次数组,一旦创建直到类析构才会被delete
随着使用,发现了一个巨大的问题:
for (auto i = 1; i < msg_count; i++)
{
auto info = vt[i - 1];
auto& rspinfo = *(RspOnInfoContent*)info.GetRspOnInfoContent();
iovecs[i].iov_len = info.RspOnInfoContentSize();
iovecs[i].iov_base = info.GetRspOnInfoContent();
infos_size += info.RspOnInfoContentSize();
}
注意到这句:
auto info = vt[i - 1];
我们在一个循环生命周期内创建了 info, 随后把info.GetRspOnInfoContent()返回的void*保存了。
注意尽管vt中的对象仍然长期存在,但是info已经在一个循环内被析构
注意每一个class都有一个Copy Constructor来实现例如 info = vt[i-1]这样的操作。如果class没有做,那么Compiler会给你写一个。
class Info 也是使用了Compiler的默认Copy Constructor.
于是 _nbytes 也被复制到 info, 随后info调用析构函数,把_nbytes的内存删除, 也就是说此时vt[i-1]中的_nbytes指向了一片失效的内存。
虽然使用 auto info = vt[i-1]本身就不科学,可以用 auto& info = vt[i-1]
但是更重要的是修改Info的逻辑
添加Info(const Info& a_other)这个函数
Info(const Info& a_info)
{
_name = a_info._name;
_nbytes = NULL;
}