visual-c++ - 如何将 GetProfileBinary 的结果保存到智能指针中?

标签 visual-c++ mfc smart-pointers code-analysis

目前我在类中有以下成员变量:

BYTE *m_pbyImportColumnMappings;

在其中一个类中,我们尝试从注册表中读取现有数据,如果不存在,我们将分配它。到目前为止,我已经将其更改为这样:

void CImportOCLMAssignmentHistoryDlg::ReadSettings()
{
    UINT uSize;

    m_dwImportFlags = theApp.GetNumberSetting(theApp.GetActiveScheduleSection(_T("Options")),
        _T("ImportFlags"), ImportAssignment::None);

    theApp.GetProfileBinary(theApp.GetActiveScheduleSection(_T("Options")), 
        _T("ImportColumnMappings"), (LPBYTE*)&m_pbyImportColumnMappings, &uSize);
    // Reset memory buffer (if required)
    if (uSize != (sizeof(BYTE) * 15))
    {
        if (uSize > 0)
        {
            delete[] m_pbyImportColumnMappings;
            m_pbyImportColumnMappings = nullptr;
        }

        m_pbyImportColumnMappings = new BYTE[15];
        // Default values
        const gsl::span column_mappings(m_pbyImportColumnMappings, 15);
        std::fill(begin(column_mappings), end(column_mappings), -1);
        /*
        m_pbyImportColumnMappings[0] = -1;
        m_pbyImportColumnMappings[1] = -1;
        m_pbyImportColumnMappings[2] = -1;
        m_pbyImportColumnMappings[3] = -1;
        m_pbyImportColumnMappings[4] = -1;
        m_pbyImportColumnMappings[5] = -1;
        m_pbyImportColumnMappings[6] = -1;
        m_pbyImportColumnMappings[7] = -1;
        m_pbyImportColumnMappings[8] = -1;
        m_pbyImportColumnMappings[9] = -1;
        m_pbyImportColumnMappings[10] = -1;
        m_pbyImportColumnMappings[11] = -1;
        m_pbyImportColumnMappings[12] = -1;
        m_pbyImportColumnMappings[13] = -1;
        m_pbyImportColumnMappings[14] = -1;
        */

    }
}

我最初的更改是使用 gsl::span 来抑制有关使用指针算术的几个警告。但考虑到我们最初尝试从 GetProfileBinary 填充它,我不知道如何将 m_pbyImportColumnMappings 转换为智能指针。

如果我可以将它变成一个智能指针,那么当类超出范围时我就不需要释放内存。

在相关答案中建议使用此代码:

theApp.GetProfileBinary(strSection, strEntry,
        reinterpret_cast<LPBYTE*>(&pALI), &uBytesRead);
std::unique_ptr<BYTE[]> cleanup(reinterpret_cast<BYTE*>(pALI)); 

但是,考虑到我们正在处理类的成员变量而不是函数中的独立变量,我不确定如何应用该 cleanup 方法。

最佳答案

为了获得更清晰的代码,请考虑使用 std::vector 和临时缓冲区

std::vector<BYTE> m_mapping;
m_mapping.resize(15, -1);
...

UINT len = 0;
BYTE* temp = nullptr;
AfxGetApp()->GetProfileBinary(_T("setting"), _T("key"), &temp, &len);
std::unique_ptr<BYTE[]> cleanup(temp);
if (len == m_mapping.size() * sizeof(m_mapping[0]))
    memcpy(m_mapping.data(), temp, len);
else
    std::fill(m_mapping.begin(), m_mapping.end(), -1);

std::vector 还具有自动清理和其他方法。


否则,使用std::unique_ptr来替换该成员数据的new/delete,可能会是一场噩梦。示例:

m_mapping = nullptr;
GetProfileBinary("setting", "key", &m_mapping, &uSize);
if (uSize != (sizeof(BYTE) * 15))
{
    { std::unique_ptr<BYTE[]> cleanup(m_mapping); }
    //delete memory immediately after exiting scope
    //note the extra brackets

    //allocate new memory and don't manage it anymore
    m_mapping = std::make_unique<BYTE[]>(15).release();
    if(m_mapping)
        for (int i = 0; i < 15; i++) m_mapping[i] = -1;
}

这里我们无法利用std::unique_ptr内存管理,它仅用于关闭警告。

此处不需要任何转换,因为 m_pbyImportColumnMappings 恰好是 BYTE,而 GetProfileBinary 需要 BYTE code>,它使用new BYTE

分配内存

关于visual-c++ - 如何将 GetProfileBinary 的结果保存到智能指针中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69719862/

相关文章:

c++ - Visual Studio 的对象文件

visual-c++ - 在不获取 LNK4006 的情况下,在 Visual C++ 中链接具有依赖项的库

visual-c++ - OpenGL 3d 立体观看航空立体图像(左、右图像)

visual-studio-2008 - 如何使用 MFC 在 MSVC 2008 中使编辑框在运行时消失(不是灰色)?

c++ - MFC:将CStringArray转换为 float ,只转换部分值

c++ - 什么是 CTime(有没有)32 位模拟?

c++ - 在没有其他智能指针的情况下创建weak_ptr

c++ - 无法打开包含文件 'afxwin.h' :no such header fileor directory in vs 2013 c++

rust - 在 Option<Rc<Struct>> 上调用 map 与在 Option<Rc<i32>> 上调用 map 的工作方式不同

带有智能指针的 C++ 智能 vector ?