cppcheck thinks there is a mismatch in alloc and dealloc
Asked Answered
G

1

6

The cpp-check error is: error mismatchAllocDealloc false Mismatching allocation and deallocation: val2

What should I do to fix this error?

void MainWindow::ParseDemo(char *pBuf)
{
  char* val2 = new char[256];
  for (int i = 0; i < 254; i++)
  {
     val2[i] = pBuf[i+305];
  }
  val2[254] = 0; // 0-Termination
  QString sunit(val2);
  DoStuff(sunit);
  delete val2;
  // ... 
}
Granado answered 4/9, 2015 at 9:41 Comment(1)
It does not say that there's a leak, it says that there's a mismatch. It would probably also say that if you wrote free(val2);.Illyrian
R
12
error mismatchAllocDealloc false Mismatching allocation and deallocation: val2

new and new [] need to be used in a consistent manner with delete and delete [], that's why cppcheck complains.

What should I do to fix this error?

Write delete [] val2;, that should fix it.

BTW, that's not indicating a memory leak directly, but it could become one easily, because it's basically undefined behavior.

Resplendent answered 4/9, 2015 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.