从新分配更改为smartpointer

时间:2015-06-09 14:35:34

标签: c++ winapi

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)。有一个完全被误解的智能指针。程序出现堆错误....

2 个答案:

答案 0 :(得分:2)

make_unique<wchar_t>(newsize)仅为单个wchar_t分配内存,您需要的是make_unique<wchar_t[]>(newsize)

答案 1 :(得分:2)

LPWSTR wcSerialNumber = new wchar_t[newsize];
auto wcSerialNumber = make_unique<wchar_t>(newsize);

这两行做的事情非常不同。第一个动态分配wchar_t个数组,其中包含多个等于newsize的元素。第二行动态分配 wchar_t,以newsize为参数调用构造函数。

在这种情况下,

std::unique_ptr不是最佳选择。动态数组最好用std::vector替换。在您的情况下,这可能看起来像:

std::vector<wchar_t> wcSerialNumber (newsize);

然后,不要致电wcSerialNumber.get(),而是致电wcSerialNumber.data()