heap error after changing from new allocation to smartpointer

81 views Asked by At
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;
}

To change this code to use smart pointers. My code look like this

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

The code compile but at run time i get _CrtIsValidHeapPointer(pUserData). Have a totally misunderstood smart pointers. The program is getting a heap error....

2

There are 2 answers

0
TartanLlama On BEST ANSWER
LPWSTR wcSerialNumber = new wchar_t[newsize];
auto wcSerialNumber = make_unique<wchar_t>(newsize);

Those two lines do very different things. The first dynamically allocates an array of wchar_ts with a number of elements equal to newsize. The second line dynamically allocates a single wchar_t, calling the constructor with newsize as an argument.

std::unique_ptr is not the best option in this case. Dynamic arrays are better replaced with std::vector. In your case, this might look like:

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

Then instead of calling wcSerialNumber.get(), call wcSerialNumber.data().

0
yngccc On

make_unique<wchar_t>(newsize) allocates memory for a single wchar_t only, what you need is make_unique<wchar_t[]>(newsize)