C++ Class 构造时由于LazyMode 和 Copy Constructor导致的问题

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; 
    }

posted on   norsd  阅读(2)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示