[英]Using std::make_unique with the GetProfileBinary function call
I have seen this answer ( Advantages of using std::make_unique over new operator ) where it states:我已经看到这个答案( Advantages of using std::make_unique over new operator ),它指出:
Don't use
make_unique
if you need a custom deleter or are adopting a raw pointer from elsewhere .如果您需要自定义删除器或正在采用来自其他地方的原始指针,请不要使用make_unique
。
This is is my code:这是我的代码:
void CAutomaticBackupSettingsPage::GetLastBackupDate(COleDateTime& rBackupDate)
{
DATE* pDatTime = nullptr;
UINT uSize;
theApp.GetProfileBinary(_T("Options"), _T("BackupLastBackupDate"), pointer_cast<LPBYTE*>(&pDatTime), &uSize);
if (uSize == sizeof(DATE))
rBackupDate = *pDatTime;
else
rBackupDate = COleDateTime::GetCurrentTime();
delete[] pDatTime;
pDatTime = nullptr;
}
Code analysis gives me two warnings:代码分析给了我两个警告:
and和
The latter warning suggests I use std::make_unique
but since my pointer data is returned from the GetProfileBinary
call, and given the statement in the related question, does that mean I should not use std::make_unique
?后一个警告建议我使用std::make_unique
但由于我的指针数据是从GetProfileBinary
调用返回的,并且给出了相关问题中的语句,这是否意味着我不应该使用std::make_unique
? I admit it is something I have not done before.我承认这是我以前没有做过的事情。
The useage of GetProfileBinary
clearly states: GetProfileBinary
的用法明确指出:
GetProfileBinary
allocates a buffer and returns its address in*ppData
.GetProfileBinary
分配一个缓冲区并在*ppData
中返回它的地址。 The caller is responsible for freeing the buffer usingdelete[]
.调用者负责使用delete[]
释放缓冲区。
pDateTime
is supposed to be nullptr
, and GetProfileBinary
handles the allocation. pDateTime
应该是nullptr
,并且GetProfileBinary
处理分配。 Code Analysis mistakenly thinks you forgot the allocation.代码分析误以为你忘记了配置。
It does need to check for success before calling delete[]
.它确实需要在调用delete[]
之前检查是否成功。 We can't use delete[]pDatTime
because pDatTime
is not an array.我们不能使用delete[]pDatTime
因为pDatTime
不是数组。 But GetProfileBinary
allocates using new BYTE[size]
, so we need to cast back to BYTE
.但是GetProfileBinary
使用new BYTE[size]
分配,所以我们需要转换回BYTE
。
You can also add a NULL
check before reading pDatTime
, that might make Code Analysis happy.您还可以在阅读pDatTime
之前添加一个NULL
检查,这可能会让代码分析感到高兴。
if (pDatTime && uSize == sizeof(DATE))
rBackupDate = *pDatTime;
else
rBackupDate = COleDateTime::GetCurrentTime();
if(pDatTime) delete[](BYTE*)pDatTime;
You can use std::unique_ptr<BYTE[]> cleanup((BYTE*)pDatTime)
for deletion, but this has to be after GetProfileBinary
is called.您可以使用std::unique_ptr<BYTE[]> cleanup((BYTE*)pDatTime)
进行删除,但这必须在调用GetProfileBinary
之后进行。
Example:例子:
DATE* pDatTime = nullptr;
GetProfileBinary(_T("Options"), _T("BackupLastBackupDate"), (LPBYTE*)(&pDatTime), &uSize);
std::unique_ptr<BYTE[]> cleanup((BYTE*)pDatTime); //automatic delete
if (pDatTime && uSize == sizeof(DATE))
rBackupDate = *pDatTime;
else
rBackupDate = COleDateTime::GetCurrentTime();
//pDatTime = NULL; <- Error when used with unique_ptr
...
//pDatTime is deleted later, when `cleanup` goes out of scope
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.