从新分配更改为智能指针后出现堆错误
heap error after changing from new allocation to smartpointer
extern "C" int __stdcall calcA(
LPCSTR SerialNumber,
double Diameter,
int Design,
int FoilSpacing,
double RotorSpeed,
double Altitude,
double SupplyAirInletTemperature,
double SupplyAirInletMoistureContent,
double ExhaustAirInletTemperature,
double ExhaustAirInletMoistureContent,
double SupplyAirOutletFlow,
double ExhaustAirInletFlow,
double PressureDifference,
bool PurgingSector,
double* PowerTotal,
double* PowerSensibel,
double* TemperatureEfficiency,
double* HumidityEfficiency,
double* SupplyAirOutletTemperature,
double* SupplyAirOutletMoistureContent,
double* ExhaustAirOutletTemperature,
double* ExhaustAirOutletMoistureContent,
double* SupplyAirOutletVelocity,
double* ExhaustAirInletVelocity,
double* SupplyAirSidePressureDrop,
double* ExhaustAirSidePressureDrop,
double* HumidifyingDehumidifying,
double* ExcessWater,
double* ExhaustAirTransferRatio,
double* OutdoorAirCorrectionFactor,
bool* Frostrisk,
double* CarryOverAirFlow)
{
int returnValue = 0;
if (NULL == SerialNumber)
{
returnValue = NULL_POINTER;
}
else
{
size_t newsize = strlen(SerialNumber) + 1; /* Including NULL */
LPWSTR wcSerialNumber = new wchar_t[newsize];
size_t convertedChars = 0;
if ((0 != mbstowcs_s(&convertedChars, wcSerialNumber, newsize, SerialNumber, _TRUNCATE)) || (convertedChars != newsize))
{
returnValue = INVALID_STRING_CONVERSION;
}
if (0 == returnValue)
{
returnValue = calcW(wcSerialNumber,
Diameter,
Design,
FoilSpacing,
RotorSpeed,
Altitude,
SupplyAirInletTemperature,
SupplyAirInletMoistureContent,
ExhaustAirInletTemperature,
ExhaustAirInletMoistureContent,
SupplyAirOutletFlow,
ExhaustAirInletFlow,
PressureDifference,
PurgingSector,
PowerTotal,
PowerSensibel,
TemperatureEfficiency,
HumidityEfficiency,
SupplyAirOutletTemperature,
SupplyAirOutletMoistureContent,
ExhaustAirOutletTemperature,
ExhaustAirOutletMoistureContent,
SupplyAirOutletVelocity,
ExhaustAirInletVelocity,
SupplyAirSidePressureDrop,
ExhaustAirSidePressureDrop,
HumidifyingDehumidifying,
ExcessWater,
ExhaustAirTransferRatio,
OutdoorAirCorrectionFactor,
Frostrisk,
CarryOverAirFlow);
}
delete[] wcSerialNumber;
}
return returnValue;
}
要更改此代码以使用智能指针。我的代码看起来像这样
extern "C" int __stdcall calcA(
LPCSTR SerialNumber,
double Diameter,
int Design,
int FoilSpacing,
double RotorSpeed,
double Altitude,
double SupplyAirInletTemperature,
double SupplyAirInletMoistureContent,
double ExhaustAirInletTemperature,
double ExhaustAirInletMoistureContent,
double SupplyAirOutletFlow,
double ExhaustAirInletFlow,
double PressureDifference,
bool PurgingSector,
double* PowerTotal,
double* PowerSensibel,
double* TemperatureEfficiency,
double* HumidityEfficiency,
double* SupplyAirOutletTemperature,
double* SupplyAirOutletMoistureContent,
double* ExhaustAirOutletTemperature,
double* ExhaustAirOutletMoistureContent,
double* SupplyAirOutletVelocity,
double* ExhaustAirInletVelocity,
double* SupplyAirSidePressureDrop,
double* ExhaustAirSidePressureDrop,
double* HumidifyingDehumidifying,
double* ExcessWater,
double* ExhaustAirTransferRatio,
double* OutdoorAirCorrectionFactor,
bool* Frostrisk,
double* CarryOverAirFlow)
{
int returnValue = 0;
if (NULL == SerialNumber)
{
returnValue = NULL_POINTER;
}
else
{
size_t newsize = strlen(SerialNumber) + 1; /* Including NULL */
auto wcSerialNumber = make_unique<wchar_t>(newsize);
size_t convertedChars = 0;
if ((0 != mbstowcs_s(&convertedChars, wcSerialNumber.get(), newsize, SerialNumber, _TRUNCATE)) || (convertedChars != newsize))
{
returnValue = INVALID_STRING_CONVERSION;
}
if (0 == returnValue)
{
returnValue = calcW(wcSerialNumber.get(),
Diameter,
Design,
FoilSpacing,
RotorSpeed,
Altitude,
SupplyAirInletTemperature,
SupplyAirInletMoistureContent,
ExhaustAirInletTemperature,
ExhaustAirInletMoistureContent,
SupplyAirOutletFlow,
ExhaustAirInletFlow,
PressureDifference,
PurgingSector,
PowerTotal,
PowerSensibel,
TemperatureEfficiency,
HumidityEfficiency,
SupplyAirOutletTemperature,
SupplyAirOutletMoistureContent,
ExhaustAirOutletTemperature,
ExhaustAirOutletMoistureContent,
SupplyAirOutletVelocity,
ExhaustAirInletVelocity,
SupplyAirSidePressureDrop,
ExhaustAirSidePressureDrop,
HumidifyingDehumidifying,
ExcessWater,
ExhaustAirTransferRatio,
OutdoorAirCorrectionFactor,
Frostrisk,
CarryOverAirFlow);
}
}
return returnValue;
}
代码编译但在 运行 时我得到 _CrtIsValidHeapPointer(pUserData)。有一个完全被误解的智能指针。程序出现堆错误....
make_unique<wchar_t>(newsize)
只为单个wchar_t
分配内存,你需要的是make_unique<wchar_t[]>(newsize)
LPWSTR wcSerialNumber = new wchar_t[newsize];
auto wcSerialNumber = make_unique<wchar_t>(newsize);
这两行做的事情截然不同。第一个动态分配 wchar_t
的数组,其中元素数等于 newsize
。第二行动态分配一个singlewchar_t
,以newsize
为参数调用构造函数
在这种情况下,std::unique_ptr
不是最佳选择。动态数组最好用 std::vector
代替。在您的情况下,这可能看起来像:
std::vector<wchar_t> wcSerialNumber (newsize);
然后不调用 wcSerialNumber.get()
,而是调用 wcSerialNumber.data()
.
extern "C" int __stdcall calcA(
LPCSTR SerialNumber,
double Diameter,
int Design,
int FoilSpacing,
double RotorSpeed,
double Altitude,
double SupplyAirInletTemperature,
double SupplyAirInletMoistureContent,
double ExhaustAirInletTemperature,
double ExhaustAirInletMoistureContent,
double SupplyAirOutletFlow,
double ExhaustAirInletFlow,
double PressureDifference,
bool PurgingSector,
double* PowerTotal,
double* PowerSensibel,
double* TemperatureEfficiency,
double* HumidityEfficiency,
double* SupplyAirOutletTemperature,
double* SupplyAirOutletMoistureContent,
double* ExhaustAirOutletTemperature,
double* ExhaustAirOutletMoistureContent,
double* SupplyAirOutletVelocity,
double* ExhaustAirInletVelocity,
double* SupplyAirSidePressureDrop,
double* ExhaustAirSidePressureDrop,
double* HumidifyingDehumidifying,
double* ExcessWater,
double* ExhaustAirTransferRatio,
double* OutdoorAirCorrectionFactor,
bool* Frostrisk,
double* CarryOverAirFlow)
{
int returnValue = 0;
if (NULL == SerialNumber)
{
returnValue = NULL_POINTER;
}
else
{
size_t newsize = strlen(SerialNumber) + 1; /* Including NULL */
LPWSTR wcSerialNumber = new wchar_t[newsize];
size_t convertedChars = 0;
if ((0 != mbstowcs_s(&convertedChars, wcSerialNumber, newsize, SerialNumber, _TRUNCATE)) || (convertedChars != newsize))
{
returnValue = INVALID_STRING_CONVERSION;
}
if (0 == returnValue)
{
returnValue = calcW(wcSerialNumber,
Diameter,
Design,
FoilSpacing,
RotorSpeed,
Altitude,
SupplyAirInletTemperature,
SupplyAirInletMoistureContent,
ExhaustAirInletTemperature,
ExhaustAirInletMoistureContent,
SupplyAirOutletFlow,
ExhaustAirInletFlow,
PressureDifference,
PurgingSector,
PowerTotal,
PowerSensibel,
TemperatureEfficiency,
HumidityEfficiency,
SupplyAirOutletTemperature,
SupplyAirOutletMoistureContent,
ExhaustAirOutletTemperature,
ExhaustAirOutletMoistureContent,
SupplyAirOutletVelocity,
ExhaustAirInletVelocity,
SupplyAirSidePressureDrop,
ExhaustAirSidePressureDrop,
HumidifyingDehumidifying,
ExcessWater,
ExhaustAirTransferRatio,
OutdoorAirCorrectionFactor,
Frostrisk,
CarryOverAirFlow);
}
delete[] wcSerialNumber;
}
return returnValue;
}
要更改此代码以使用智能指针。我的代码看起来像这样
extern "C" int __stdcall calcA(
LPCSTR SerialNumber,
double Diameter,
int Design,
int FoilSpacing,
double RotorSpeed,
double Altitude,
double SupplyAirInletTemperature,
double SupplyAirInletMoistureContent,
double ExhaustAirInletTemperature,
double ExhaustAirInletMoistureContent,
double SupplyAirOutletFlow,
double ExhaustAirInletFlow,
double PressureDifference,
bool PurgingSector,
double* PowerTotal,
double* PowerSensibel,
double* TemperatureEfficiency,
double* HumidityEfficiency,
double* SupplyAirOutletTemperature,
double* SupplyAirOutletMoistureContent,
double* ExhaustAirOutletTemperature,
double* ExhaustAirOutletMoistureContent,
double* SupplyAirOutletVelocity,
double* ExhaustAirInletVelocity,
double* SupplyAirSidePressureDrop,
double* ExhaustAirSidePressureDrop,
double* HumidifyingDehumidifying,
double* ExcessWater,
double* ExhaustAirTransferRatio,
double* OutdoorAirCorrectionFactor,
bool* Frostrisk,
double* CarryOverAirFlow)
{
int returnValue = 0;
if (NULL == SerialNumber)
{
returnValue = NULL_POINTER;
}
else
{
size_t newsize = strlen(SerialNumber) + 1; /* Including NULL */
auto wcSerialNumber = make_unique<wchar_t>(newsize);
size_t convertedChars = 0;
if ((0 != mbstowcs_s(&convertedChars, wcSerialNumber.get(), newsize, SerialNumber, _TRUNCATE)) || (convertedChars != newsize))
{
returnValue = INVALID_STRING_CONVERSION;
}
if (0 == returnValue)
{
returnValue = calcW(wcSerialNumber.get(),
Diameter,
Design,
FoilSpacing,
RotorSpeed,
Altitude,
SupplyAirInletTemperature,
SupplyAirInletMoistureContent,
ExhaustAirInletTemperature,
ExhaustAirInletMoistureContent,
SupplyAirOutletFlow,
ExhaustAirInletFlow,
PressureDifference,
PurgingSector,
PowerTotal,
PowerSensibel,
TemperatureEfficiency,
HumidityEfficiency,
SupplyAirOutletTemperature,
SupplyAirOutletMoistureContent,
ExhaustAirOutletTemperature,
ExhaustAirOutletMoistureContent,
SupplyAirOutletVelocity,
ExhaustAirInletVelocity,
SupplyAirSidePressureDrop,
ExhaustAirSidePressureDrop,
HumidifyingDehumidifying,
ExcessWater,
ExhaustAirTransferRatio,
OutdoorAirCorrectionFactor,
Frostrisk,
CarryOverAirFlow);
}
}
return returnValue;
}
代码编译但在 运行 时我得到 _CrtIsValidHeapPointer(pUserData)。有一个完全被误解的智能指针。程序出现堆错误....
make_unique<wchar_t>(newsize)
只为单个wchar_t
分配内存,你需要的是make_unique<wchar_t[]>(newsize)
LPWSTR wcSerialNumber = new wchar_t[newsize];
auto wcSerialNumber = make_unique<wchar_t>(newsize);
这两行做的事情截然不同。第一个动态分配 wchar_t
的数组,其中元素数等于 newsize
。第二行动态分配一个singlewchar_t
,以newsize
为参数调用构造函数
std::unique_ptr
不是最佳选择。动态数组最好用 std::vector
代替。在您的情况下,这可能看起来像:
std::vector<wchar_t> wcSerialNumber (newsize);
然后不调用 wcSerialNumber.get()
,而是调用 wcSerialNumber.data()
.